# Publishing

Publish for every customer with an idempotency key, one media upload per asset, each customer's own queue and outcomes from webhooks.

import { Tab, Tabs } from 'fumadocs-ui/components/tabs';

By the end of this page, your app creates posts for every customer without duplicates. It uploads each file once, spreads publishing across each customer's queue and learns the outcome from webhooks.

## Before you start

A duplicate here goes out on your customer's account, which is why each step is retry-safe.

You need a customer's `profileId` and their account ids from the [core model](/multi-tenant).

## Step 1: Upload media once

Call `POST /v1/media/presign`, `PUT` the file to `uploadUrl`, and put `publicUrl` in the post's `mediaItems`.

One upload serves every platform entry in the post and can be reused across posts, except with [zero content retention](/security/zero-content-retention).

Uploads sit in temporary storage for 7 days. They are copied to permanent storage when you create the post that uses them.

If your team or any profile uses zero content retention, an upload is single use: deleted once a post uses it, or after 1 day unused.

<Callout type="warn">
Create the post within 7 days of the upload (1 day with zero content retention). Or upload when you create the post, rather than when the customer picks the file.
</Callout>

The [media uploads guide](/guides/media-uploads) has the full flow.

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
import { readFile } from 'node:fs/promises';
import { randomUUID } from 'crypto';
import Zernio from '@zernio/node';

const zernio = new Zernio();
const profileId = '66a1f0c2a4b9d3e8f1a2b3c4';

const { data: presigned } = await zernio.media.getMediaPresignedUrl({
  body: { filename: 'launch.mp4', contentType: 'video/mp4' },
});

const fileBuffer = await readFile('launch.mp4');

await fetch(presigned.uploadUrl, {
  method: 'PUT',
  headers: { 'Content-Type': 'video/mp4' },
  body: fileBuffer,
});
```
</Tab>
<Tab value="Python">
```python
import uuid
import httpx
from zernio import Zernio

client = Zernio()
profile_id = "66a1f0c2a4b9d3e8f1a2b3c4"

presigned = client.media.get_media_presigned_url(
    filename="launch.mp4",
    content_type="video/mp4",
)

with open("launch.mp4", "rb") as f:
    httpx.put(presigned["uploadUrl"], content=f.read(), headers={"Content-Type": "video/mp4"})
```
</Tab>
<Tab value="curl">
```bash
curl -X POST "https://zernio.com/api/v1/media/presign" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "filename": "launch.mp4", "contentType": "video/mp4" }'

# UPLOAD_URL is the uploadUrl from the presign response
curl -X PUT "$UPLOAD_URL" \
  -H "Content-Type: video/mp4" \
  --data-binary @launch.mp4
```
</Tab>
</Tabs>

Response (`200`) from the presign call:

```json
{
  "uploadUrl": "https://...",
  "publicUrl": "https://media.zernio.com/temp/1234567890_abc123_launch.mp4",
  "expiresIn": 3600
}
```

Response (`200`) from the `PUT`, with no body: storage answers with an empty `200` and an `ETag` header.

From then on the file is served at `publicUrl`.

## Step 2: Create the post idempotently

Call `POST /v1/posts` with an `Idempotency-Key` header, the customer's account ids and `queuedFromProfile` set to their `profileId`.

Generate one UUID per logical post and send the same UUID on every retry of that call. A replay within 24 hours returns `200` with the original post in `post`.

**Python.** The Python SDK sends it as `x_request_id` (the `x-request-id` header). That returns the original post only when the retry's content is identical.

Treat a replay and a `409` (same content to the same account within 24 hours) as success. The [idempotency guide](/guides/idempotency) covers every layer.

`queuedFromProfile` places the post on the next free slot of that customer's [queue](/guides/queue-scheduling). Each profile drips its own posts, with no scheduler of your own.

For a post that must go out at an exact moment, send `scheduledFor` and `timezone` instead.

For a customer's CSV import, use [`POST /v1/posts/bulk-upload`](/posts/bulk-upload-posts) with `dryRun=true` first.

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
const { data: listed } = await zernio.accounts.listAccounts({
  query: { profileId },
});

const { data: queued } = await zernio.posts.createPost({
  headers: { 'Idempotency-Key': randomUUID() },
  body: {
    content: 'Big news coming Friday.',
    mediaItems: [{ type: 'video', url: presigned.publicUrl }],
    platforms: listed.accounts.map((a) => ({
      platform: a.platform,
      accountId: a._id,
    })),
    queuedFromProfile: profileId,
  },
});

console.log(queued.post.status, queued.post.scheduledFor);
```
</Tab>
<Tab value="Python">
```python
listed = client.accounts.list_accounts(profile_id=profile_id)

queued = client.posts.create_post(
    x_request_id=str(uuid.uuid4()),
    content="Big news coming Friday.",
    media_items=[{"type": "video", "url": presigned["publicUrl"]}],
    platforms=[
        {"platform": a["platform"], "accountId": a["_id"]}
        for a in listed["accounts"]
    ],
    queued_from_profile=profile_id,
)

print(queued["post"]["status"], queued["post"]["scheduledFor"])
```
</Tab>
<Tab value="curl">
```bash
curl -X POST "https://zernio.com/api/v1/posts" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 4b4986f4-77b5-4c22-a3a7-2c56f7a657e1" \
  -d '{
    "content": "Big news coming Friday.",
    "mediaItems": [{ "type": "video", "url": "https://media.zernio.com/temp/1234567890_abc123_launch.mp4" }],
    "platforms": [{ "platform": "instagram", "accountId": "66b2e19d8c3f5a7e9d0b1c2d" }],
    "queuedFromProfile": "66a1f0c2a4b9d3e8f1a2b3c4"
  }'
```
</Tab>
</Tabs>

Response (`201`):

```json
{
  "message": "Post scheduled successfully",
  "post": {
    "_id": "65f1c0a9e2b5af0012ab34cd",
    "status": "scheduled",
    "scheduledFor": "2027-01-04T14:00:00Z",
    "queuedFromProfile": "66a1f0c2a4b9d3e8f1a2b3c4",
    "platforms": [
      { "platform": "instagram", "accountId": "66b2e19d8c3f5a7e9d0b1c2d", "status": "pending" }
    ]
  }
}
```

<Callout type="warn">
`POST /v1/posts` accepts any `accountId` your team owns, whichever profile it sits in. Pass a customer only the account ids stored against them.
</Callout>

A scheduler that fires every customer's posts with `publishNow: true` on the hour creates a burst. It spends your request budget at once and runs into per-account velocity limits.

Queue slots spread the same posts across the hour.

## Step 3: Read the outcome from webhooks

The `201` means accepted. What happened at publish time arrives on the [post webhooks](/webhooks/social):

- `post.published` when every platform succeeded.
- `post.partial` or `post.failed` otherwise.
- `post.platform.published` / `post.platform.failed` per platform, as each one finishes.

Route each event to the customer with `profiles[0].externalId` (a post that spans several customers' profiles lists each one). Then update your UI from `post.status` ([post lifecycle](/guides/post-lifecycle)).

Do not poll `GET /v1/posts` to discover status changes. Polling is for reconciliation.

<Mermaid
  chart={`%%{init: {"themeVariables": {"signalColor": "#ec3013"}}}%%
sequenceDiagram
  participant T as Customer
  participant A as Your app
  participant Z as Zernio
  T->>A: writes post, hits Publish
  A->>Z: POST /v1/posts (Idempotency-Key, queuedFromProfile)
  Z-->>A: 201, status: scheduled, scheduledFor
  Note over Z: the slot arrives and Zernio publishes
  alt every platform succeeds
    Z-->>A: webhook post.published (per-platform URLs)
    A-->>T: show live post links
  else some or all fail
    Z-->>A: webhook post.partial / post.failed (error per platform)
    A-->>T: surface the platform error and offer a retry
  end`}
/>

A `post.published` delivery for the post above:

```json
{
  "id": "evt_5f8e2a1c",
  "event": "post.published",
  "post": {
    "id": "65f1c0a9e2b5af0012ab34cd",
    "status": "published",
    "scheduledFor": "2027-01-04T14:00:00Z",
    "publishedAt": "2027-01-04T14:00:03Z",
    "platforms": [
      {
        "platform": "instagram",
        "accountId": "66b2e19d8c3f5a7e9d0b1c2d",
        "status": "published",
        "publishedUrl": "https://www.instagram.com/p/..."
      }
    ]
  },
  "timestamp": "2027-01-04T14:00:03Z"
}
```

If a published post is later deleted on the platform, `post.platform.deleted` fires (poll-driven, roughly hourly). Your UI can then stop showing it as live.

## If it fails

A `409` from Step 2 means the same content is already scheduled or was posted to that account in the last 24 hours:

```json
{
  "error": "This exact content is already scheduled, publishing, or was posted to this account within the last 24 hours.",
  "details": {
    "accountId": "66b2e19d8c3f5a7e9d0b1c2d",
    "platform": "instagram",
    "existingPostId": "65f1c0a9e2b5af0012ab34cd"
  }
}
```

Treat it as done and show the customer `existingPostId`. To repost the same content on purpose, change the caption, the media or the account.

## Related

- [Build a platform](/multi-tenant): the profile-per-customer model.
- [Idempotency](/guides/idempotency): `Idempotency-Key`, `x-request-id` and content-hash dedup in depth.
- [Queue scheduling](/guides/queue-scheduling): slots, several queues per profile, previewing the next slot.
- [Media uploads](/guides/media-uploads): presigned URLs, size limits, per-platform rules.
- [Post lifecycle](/guides/post-lifecycle): every status and transition.