> Index of every page: https://docs.merchantservicedepot.com/llms.txt

# Idempotency and retries

> Send the same request twice without charging twice.

A network failure leaves you not knowing whether a request landed. Send an `Idempotency-Key` header and you can send the same request again: Myela recognizes the repeat and returns the original result instead of moving money twice.

## How keys work

The key is any unique string you choose, sent as a request header. Use one key per logical operation, not one per attempt: the order you're charging gets a key, and every retry of that charge reuses it.

```bash title="terminal"
curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/payments \
  -H "Authorization: Bearer myela_sk_test_…" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order_1001" \
  -d '{ "amount": 4999, "currency": "USD", "paymentToken": "…" }'
```

- Keys are scoped to your account. Another merchant's key never collides with yours.
- Myela doesn't compare request bodies. Reuse a key with a different amount and you get the original payment back, unchanged.
- Without a key, every request is new. A retried charge with no key is a second charge.

## Where keys apply

| Route | Key | A repeat with the same key |
| --- | --- | --- |
| `POST /v1/payments` | Optional | Returns the original payment, whatever its final status. |
| `POST /v1/payments/{id}/capture` | Optional | Returns the payment if the capture succeeded. |
| `POST /v1/invoices` | Optional | Returns the invoice in its current state, with the original status code. |
| `POST /v1/invoices/{id}/schedule` | Optional | Returns the invoice in its current state. A later schedule under a different key replaces this key. |
| `POST /v1/bulk_jobs/customers, /captures, /invoices` | Required | Returns the existing job, and resumes it if it hadn't finished. |
| `POST /v1/credits` | Required | None today. The route answers 501 on every account. |
| `POST /v1/payments/{id}/void, /refund` | Not read | None. Read the payment before you retry. |

A repeated create returns the payment with `201`, even when the first attempt was declined or failed. Check its `status`, not the HTTP code. On bulk jobs, each capture or invoice row can carry its own `idempotencyKey` as well.

On invoices, a key can be up to 255 characters, and an empty key counts as none. Unlike a payment key, an invoice key is checked against the request body: the same key with a different body is refused. Keys are separate per operation, so one key may name both an invoice's create and its schedule.

## Replays that conflict

When a repeat can't return a finished result, Myela answers `409 conflict` with a `details.code`:

| details.code | When |
| --- | --- |
| `IDEMPOTENCY_KEY_REUSED` | The key was already used for a different capture, or for a different type of bulk job. On an invoice, the same key was sent with a different body, or a schedule key is held by another invoice. |
| `IDEMPOTENT_ATTEMPT_FAILED` | An earlier capture with this key was refused. Retry with a new key. |
| `IDEMPOTENT_ATTEMPT_UNRESOLVED` | An earlier request with this key hasn't resolved. Don't start over under a new key. |

`IDEMPOTENT_ATTEMPT_UNRESOLVED` means the first attempt's outcome isn't known yet, and the key stays blocked until it is. On a capture, read the payment with `GET /v1/payments/{id}` before you decide anything. On a create, don't retry under a fresh key: that starts a second payment.

## Void and refund

Void and refund don't read the key. A repeated void or refund is a new request. If one fails in a way you can't read, a timeout or a dropped connection, fetch the payment with `GET /v1/payments/{id}` and check its `status` and `amountRefunded` before you call again. More in [Capture, void, and refund](https://docs.merchantservicedepot.com/docs/payments/capture-void-refund/).

### Retrying a cancel

- `POST /v1/payment_links/{id}/cancel` on a cancelled link returns the link, no error.
- `POST /v1/invoices/{id}/cancel_schedule` succeeds again and clears the collection.
- `POST /v1/recurring_invoices/{id}/cancel` on a cancelled schedule returns the schedule, no error.

## When to retry

Retry `api_error` and `rate_limited`. Every other code fails the same way each time; see [Errors](https://docs.merchantservicedepot.com/docs/platform/errors/). When you retry a create or a capture, reuse the same key. Two cases need care:

- A create refused with `rate_limited` can be retried under the same key. The retry makes a new attempt.
- A capture that was refused, `rate_limited` included, is final under its key. A repeat returns `409`, so retry a refused capture with a new key.

## What the SDK retries

The Node SDK, `@myela/payments`, retries some failures on its own and attaches a key so repeats are recognized. You can pass your own key with the `idempotencyKey` option.

| Setting | Behavior |
| --- | --- |
| `maxRetries` | 2 by default, so 3 attempts in total |
| `Timeout` | 30 seconds per attempt by default |
| `Backoff` | Random, up to 8 seconds, growing with each attempt |
| `Retried` | 429, any 5xx except 501, network failures, timeouts |
| `Not retried` | Any other 4xx, 501, and requests you abort |
| `Idempotency-Key` | Generated for every non-GET request, the same on each attempt |

The SDK sends a key on every POST, but the server reads it only on create, capture, invoice create and schedule, bulk jobs, and credits. If a void or refund call through the SDK ends in an error, read the payment before you retry it.

Source: https://docs.merchantservicedepot.com/docs/platform/idempotency/
