---
title: "Idempotency"
description: "Retry any write safely with an Idempotency-Key, so a network error never posts twice."
---

> Documentation Index
> Fetch the complete documentation index at: https://pubnio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotency

A network call can fail after PubNio already did the work. If you simply send
it again, you might post twice. An idempotency key fixes that: send the same
key with the same request, and PubNio returns the first result instead of
doing the work again.

## How it works

Send the key in the `Idempotency-Key` header. A key is 1 to 128 characters of
letters, digits, `.`, `_`, `~`, or `-`. A UUID works well.

| You send | PubNio returns |
| - | - |
| A new key | Does the work and stores the result for 24 hours. |
| The same key and the same body | The stored result. Nothing runs twice. |
| The same key while the first call is still going | `409`. Retry the same request to read its result. |
| The same key with a different body | `409` with `https://pubnio.com/docs/problems/idempotency-conflict/`. |
| The same key after 24 hours | Treated as a new key. |

One key means one intent, such as "publish this post to these accounts." Use a
new key when the intent changes, even slightly. Reuse the key only to retry the
exact same call.

## Where keys apply

- **Required:** publishing a saved draft, importing media by URL, starting a
  resumable upload, and inbox replies and messages.
- **Optional:** creating a post, commenting, inline media uploads, workspace
  invitations, and billing checkout. When you create a post without a key,
  PubNio generates one and returns it in the response as `idempotencyKey`.
- **Ads writes** require a key. The same key and intent return the same
  operation, so a retried campaign create never makes a second campaign.

Reads never need a key. They are always safe to repeat.

## Retry a publish

The SDKs generate a key for every publish and return it on the result. Keep it
with your record, and pass it again if you need to retry the same call.

### TypeScript

```ts
const accepted = await workspace.posts.create({
  text: "Spring trail guide is live.",
  platforms: ["instagram", "threads"],
  idempotencyKey: `campaign-42-post-7`,
});
// Store accepted.idempotencyKey. A retry with the same key and body
// returns this same postId.
```

### Python

```python
accepted = workspace.posts.create(
    text="Spring trail guide is live.",
    platforms=["instagram", "threads"],
    idempotency_key="campaign-42-post-7",
)
# A retry with the same key and body returns this same postId.
```

### cURL

```sh
curl -X POST "https://pubnio.com/api/v1/posts?workspaceId=$WORKSPACE_ID" \
  -H "Authorization: Bearer $PUBNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: campaign-42-post-7" \
  -d '{"text":"Spring trail guide is live.","platforms":["instagram","threads"]}'
```

### CLI

```sh
pnio posts publish --workspace "$WORKSPACE_ID" "$POST_ID" \
  --revision 1 --idempotency-key campaign-42-post-7
```

Build the key from your own IDs, like a campaign and post number, so a crashed
worker that restarts sends the same key without having to remember it.

## Duplicate content

A new key is a new intent, so it cannot replay an earlier post. It also cannot
repeat one by accident. When the same account and placement already received
the same text and media in the last 24 hours, creating
or publishing a post returns `409 duplicate_content`. The problem names
`existingPostId` and lists every match, and nothing is created. Settings and
times do not count, so changing only a setting is still a duplicate. An
earlier post that failed or was cancelled does not block.

Read `existingPostId` before posting again. To repeat the content on purpose,
send the same request with `allowDuplicate: true`. The `409` left your key
unused, so you can keep it. In the web app, **Post anyway** sends that request
for you. Validate a post first to see the match early as a
`duplicate_content` warning.

## Built-in retries

The TypeScript and Python SDKs retry reads and keyed writes up to two times
with backoff. They never retry a write without a key. The CLI and hosted MCP
use the same SDK, so they behave the same way.

## Errors

- **Conflict, still in progress:** the first call has not finished. Wait a
  moment and send the same request again.
- **Idempotency conflict:** the key was already used for a different body.
  Pick a new key for the new intent.
- **Provider issue:** the network or provider failed. Retry with the same key
  once the issue clears; PubNio will not run the work twice.

Retrying a failed delivery is a different action from retrying a request. See
[Post lifecycle](https://pubnio.com/docs/concepts/post-lifecycle/) for target retries and reconciles,
and [Idempotent webhook handling](https://pubnio.com/docs/guides/idempotent-webhooks/) for the
receiving side.

Source: https://pubnio.com/docs/concepts/idempotency/index.mdx
