Safe retries

Send an Idempotency-Key with every change, and a request that is sent twice still runs only once.

Download all as MarkdownOpenAPI

Why

A request can go through while its answer gets lost: a timeout, a dropped connection, a crash on your side. If you send it again, you may list the same tickets twice. If you do not, you may have listed none. With an idempotency key you can always send it again.

Send Idempotency-Key with every request that changes something: every POST, PATCH and DELETE. The API works without it; we recommend it everywhere and urge it for POST /v1/listings.

The value is yours to choose: up to 200 printable ASCII characters, without spaces. A random UUID works well. Use a new value for every new request, and the same value whenever you repeat one.

Request
curl -X POST "https://www.syncseats.com/api/v1/listings" \
	-H "Authorization: Bearer ssapi_test_..." \
	-H "Content-Type: application/json" \
	-H "Idempotency-Key: pos-48211-create" \
	-d '{
		"event_id": "6718d2f4a9c3b17e5d204a61",
		"listings": [
			{
				"section": "112",
				"row": "F",
				"seats": ["14", "15"],
				"price_per_ticket": { "amount": "412.00", "currency": "USD" },
				"face_value": { "amount": "195.00", "currency": "USD" },
				"cost_per_ticket": { "amount": "280.00", "currency": "USD" },
				"reference": "48211"
			}
		],
		"proof_of_purchase_upload_ids": ["upl_Hc7wPq2Lr9Vt4Kz1Xm8Bn3Fd"]
	}'

What happens on a repeat

What you sendWhat happens
The same request again, after the first went throughYou get the first answer again, with the header Idempotent-Replayed: true. Nothing runs a second time.
The same request again, after the first went through with an answer of more than 256 KB409 conflict: the answer was too large to keep. Nothing runs a second time; read the object instead.
The same request again, while the first is still running409 idempotency_in_progress with Retry-After. Send it again a little later.
The same request again, after the first was refusedIt runs again, as if it were the first time.
The same request again, after the first got 500 internal_errorYou get that answer again, with Idempotent-Replayed: true. The first may have changed something, so nothing runs a second time. Read the object, then send what is still missing with a new key.
Another request with the same key: another method, path, query or body, or another API key of your account422 idempotency_key_reused

A key is remembered for 24 hours, for your account and the mode of the key that sent it. After that, the same value counts as new. The first answer is given again only to the API key that sent the request: an answer can hold what only that key may read.

Only what went through is kept

Only an answer that says the request went through is kept and given again, and 500 internal_error, after which that is not certain. A refusal changed nothing, so its key is free again. If a change is refused with 409 listing_busy and you send the same request with the same key a moment later, it runs.

That also means a repeat can end differently than the first try. Read the answer you get, not the one you expected.

An answer is kept up to 256 KB. A larger one is not kept, so the same request with the same key gets 409 conflict instead of that answer, and does not run again: read the object to see what the request did.

Send the same bytes

Requests count as the same when they come with the same API key and their method, their path with its query and their body are the same, byte for byte. Build the body once and keep it for the repeat. A body that is put together again with its fields in another order is another request to the API, and gets the refusal for a reused key.

The second safety net

Whatever the header says, SyncSeats never lists the same seat of an event twice. A ticket group whose seats are already listed is not created again; the refusal carries the code seats_taken, for a request with several groups under skipped.

A request that is sent until it is answered

This function sends a change, repeats it when there was no answer or the refusal is worth waiting out, and gives up on a refusal that sending again cannot cure:

Node 18 or newer
import { randomUUID } from 'node:crypto'

const BASE = 'https://www.syncseats.com/api/v1'
const TRY_AGAIN = new Set(['rate_limited', 'listing_busy', 'idempotency_in_progress', 'marketplace_error', 'service_unavailable', 'api_suspended'])
const ATTEMPTS = 5

async function change(method, path, body) {
	const key = randomUUID() // one key for the request and every repeat of it
	const payload = body === undefined ? undefined : JSON.stringify(body) // the same bytes every time
	let wait = 1
	for (let attempt = 1; attempt <= ATTEMPTS; attempt++) {
		let response
		try {
			response = await fetch(BASE + path, {
				method,
				headers: {
					Authorization: 'Bearer ssapi_test_...',
					'Idempotency-Key': key,
					...(payload ? { 'Content-Type': 'application/json' } : {}),
				},
				body: payload,
			})
		} catch {
			// No answer. The request may have gone through or not: send it again.
		}
		if (response) {
			const answer = await response.json()
			if (response.ok) return answer
			if (!TRY_AGAIN.has(answer.error.code)) throw new Error(response.status + ' ' + answer.error.code)
			wait = Number(response.headers.get('Retry-After')) || wait
		}
		await new Promise(resolve => setTimeout(resolve, wait * 1000))
		wait *= 2
	}
	throw new Error('No answer after ' + ATTEMPTS + ' attempts')
}