Idempotency

Idempotency lets you safely retry a successful request leaving the outcome the same. Alpaca guarantees this for GET, PUT, and DELETE operations unless explicitly stated otherwise. POST and PATCH change state and are idempotent when you send an idempotency key. For example, if a POST request to move cash between accounts times out, Alpaca may already have created the journal. Retrying with the same idempotency key and request body lets Alpaca recognize the retry and return the existing journal without creating a second one. This guide shows how to take advantage of this property. This guide shows how to take advantage of this property.

Send an idempotency key

Use the optional Idempotency-Key header on POST and PATCH endpoints that list it in the API reference. Include it on the first attempt so you can safely retrycs and require no key for that behavior.

  1. Generate a unique UUIDv7 or UUIDv4 for each operation you intend to perform.
  2. Save the key and request body before sending the request. Send the key without quotes and preserve its capitalization.
  3. If you need to retry, use the same endpoint, account, key, and request body.
  4. Keep the original key for retries. Alpaca recognizes it for at least 7 days after the first request.
  5. Ensure each key is unique within your account or your correspondent, depending on the endpoint.
  6. Generate a fresh key for each new operation. If you retry an operation, reuse its original key and request body. Once the key expires, Alpaca treats a request using that key as a new request.

Generate keys of 36 characters or fewer. Keys longer than 36 characters are deprecated. If your integration still generates longer keys, update it to use UUIDs for new operations. For any operation already submitted, keep its original key unchanged on retries. See the idempotency key changelog.

Example: Retry a Trading API request with an idempotency key

When you request a locate for a short sale, generate a UUIDv7 or UUIDv4 and include it in the Idempotency-Key header on the first attempt. This example shows the request format for an eligible live account. Locates are unavailable in paper trading and can incur a fee. See Create Locate.

curl --request POST 'https://api.alpaca.markets/v1/locates' \
  --header "APCA-API-KEY-ID: ${APCA_API_KEY_ID}" \
  --header "APCA-API-SECRET-KEY: ${APCA_API_SECRET_KEY}" \
  --header "Idempotency-Key: ${LOCATE_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{"symbol":"TSLA","qty":100,"limit_price":"0.05"}'

If the request times out, retry with the same key and body while the key is still valid. If Alpaca already processed the request, the retry returns the original locate response without creating another locate. If you change qty while reusing the key, the API returns 422 Unprocessable Entity.

Handle retries and responses

⚠️

Retry with the original key

If a timeout, connection error, or HTTP error leaves the outcome unclear, retry with the same Idempotency-Key and request body. Sending a new key or retrying after the original key expires can perform the operation again.

If the key may have expired, verify the original outcome before resubmitting. If you have the resource ID, use the endpoint’s normal retrieval API to check its status.

ResponseWhat to do
Successful replayUse the returned resource. Where the endpoint documents Idempotency-Replayed: true, that response header identifies a replay. Depending on the endpoint, the body may contain the original result or the resource’s current state.
400 for an invalid keyRead the error and correct the key. An empty value or a value that exceeds the endpoint’s limit can cause this error.
409 Conflict because the original request is still processingIf the endpoint documents this response, wait briefly and retry with the same key and body.
422 Unprocessable Entity because the key was used with a different requestCheck the response body to determine the cause. If it identifies an idempotency conflict, use the original key and unchanged request body to retry the original operation. If it identifies a validation error, correct the reported field. If the cause is unclear, verify the original outcome before sending the request again with a new key.



Did this page help you?