# Queue scheduling

Give a profile recurring posting slots and create posts with queuedFromProfile so each one lands on the next free slot.

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

Give a profile recurring slots and create posts with `queuedFromProfile`: each lands on the next free one, so 50 posts need no `scheduledFor` values.

## Before you start

You need an API key, a profile id ([create a profile](/profiles/create-profile)) and a connected account.

## Create a queue

Call `POST /v1/queue/slots` with `profileId`, `name`, `timezone` and `slots`. Each slot is a `dayOfWeek` (0 is Sunday, 6 is Saturday) and a 24-hour `time`.

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

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

const { data: queue } = await zernio.queue.createQueueSlot({
  body: {
    profileId,
    name: 'Weekday mornings',
    timezone: 'America/New_York',
    slots: [
      { dayOfWeek: 1, time: '09:00' },
      { dayOfWeek: 3, time: '09:00' },
      { dayOfWeek: 5, time: '09:00' }
    ]
  }
});

const queueId = queue.schedule._id;
```
</Tab>
<Tab value="Python">
```python
from zernio import Zernio

client = Zernio()
profile_id = "66a1f0c2a4b9d3e8f1a2b3c4"

queue = client.queue.create_queue_slot(
    profile_id=profile_id,
    name="Weekday mornings",
    timezone="America/New_York",
    slots=[
        {"dayOfWeek": 1, "time": "09:00"},
        {"dayOfWeek": 3, "time": "09:00"},
        {"dayOfWeek": 5, "time": "09:00"},
    ],
)

queue_id = queue["schedule"]["_id"]
```
</Tab>
<Tab value="curl">
```bash
curl -X POST "https://zernio.com/api/v1/queue/slots" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "profileId": "66a1f0c2a4b9d3e8f1a2b3c4",
    "name": "Weekday mornings",
    "timezone": "America/New_York",
    "slots": [
      { "dayOfWeek": 1, "time": "09:00" },
      { "dayOfWeek": 3, "time": "09:00" },
      { "dayOfWeek": 5, "time": "09:00" }
    ]
  }'
```
</Tab>
</Tabs>

Response (`201`):

```json
{
  "success": true,
  "schedule": {
    "_id": "66d5e3bf9a1c2d4e6f7a8b9c",
    "profileId": "66a1f0c2a4b9d3e8f1a2b3c4",
    "name": "Weekday mornings",
    "timezone": "America/New_York",
    "slots": [
      { "dayOfWeek": 1, "time": "09:00" },
      { "dayOfWeek": 3, "time": "09:00" },
      { "dayOfWeek": 5, "time": "09:00" }
    ],
    "active": true,
    "isDefault": true
  },
  "nextSlots": [
    "2027-01-04T09:00:00-05:00",
    "2027-01-06T09:00:00-05:00"
  ]
}
```

- **`GET /v1/queue/slots?profileId=...`**: the default queue and its next slots. `all=true` lists every queue on the profile.
- **Update or delete slots**: the [queue endpoints](/queue/list-queue-slots).

## Queue a post

Call `POST /v1/posts` with `queuedFromProfile` set to the profile id and no `scheduledFor`. Zernio locks the next free slot; repeat per post and each takes the slot after the previous one.

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
const { data: queued } = await zernio.posts.createPost({
  body: {
    content: 'Post 1 of 50',
    platforms: [
      { platform: 'linkedin', accountId: '66b2e19d8c3f5a7e9d0b1c2d' }
    ],
    queuedFromProfile: profileId
  }
});

console.log(queued.post.scheduledFor);
```
</Tab>
<Tab value="Python">
```python
queued = client.posts.create_post(
    content="Post 1 of 50",
    platforms=[
        {"platform": "linkedin", "accountId": "66b2e19d8c3f5a7e9d0b1c2d"}
    ],
    queued_from_profile=profile_id,
)

print(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" \
  -d '{
    "content": "Post 1 of 50",
    "platforms": [
      { "platform": "linkedin", "accountId": "66b2e19d8c3f5a7e9d0b1c2d" }
    ],
    "queuedFromProfile": "66a1f0c2a4b9d3e8f1a2b3c4"
  }'
```
</Tab>
</Tabs>

Response (`201`):

```json
{
  "message": "Post scheduled successfully",
  "post": {
    "_id": "65f1c0a9e2b5af0012ab34cd",
    "content": "Post 1 of 50",
    "status": "scheduled",
    "scheduledFor": "2027-01-04T14:00:00Z",
    "timezone": "America/New_York",
    "queuedFromProfile": "66a1f0c2a4b9d3e8f1a2b3c4",
    "queueId": "66d5e3bf9a1c2d4e6f7a8b9c",
    "platforms": [
      { "platform": "linkedin", "status": "pending" }
    ]
  }
}
```

## Preview the next slot

To show when a post would publish, call `GET /v1/queue/next-slot` with `profileId`. Add `queueId` for a specific queue.

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

console.log(next.nextSlot);
```
</Tab>
<Tab value="Python">
```python
next_slot = client.queue.get_next_queue_slot(profile_id=profile_id)

print(next_slot["nextSlot"])
```
</Tab>
<Tab value="curl">
```bash
curl "https://zernio.com/api/v1/queue/next-slot?profileId=66a1f0c2a4b9d3e8f1a2b3c4" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```
</Tab>
</Tabs>

Response (`200`):

```json
{
  "profileId": "66a1f0c2a4b9d3e8f1a2b3c4",
  "nextSlot": "2027-01-06T09:00:00-05:00",
  "timezone": "America/New_York",
  "queueId": "66d5e3bf9a1c2d4e6f7a8b9c",
  "queueName": "Weekday mornings"
}
```

<Callout type="warn">
Do not pass `nextSlot` as `scheduledFor`: that skips the queue lock, so 2 concurrent creates can land on the same slot. Use `queuedFromProfile` and let Zernio assign the time.
</Callout>

## How it behaves

- **Several queues per profile.** Call `POST /v1/queue/slots` again with the same `profileId` and a different `name`.
- **Default queue.** The first queue a profile gets is its default; later ones are not until a `PUT /v1/queue/slots` with `setAsDefault: true` moves the flag.
- **Targeting.** Pass `queueId` next to `queuedFromProfile` for a specific queue. Without it, the post goes to the default queue.

## If it fails

| Error | Cause | Fix |
|---|---|---|
| `409` `queue_slot_conflict` on `PUT /v1/posts/{postId}` | The new `scheduledFor` is taken by another post in the same queue. | Pick a free time, drop `scheduledFor` so the queue assigns the next open slot, or send `queueId: null` to schedule outside the queue. |
| `404` on `GET /v1/queue/next-slot` | No queue, or no free slot. | Create a queue or add slots. |
| `400` on `GET /v1/queue/next-slot` | Queue inactive (`active: false`) or a bad parameter. | Activate it or fix the parameter. |
| `400` on `POST /v1/queue/slots` | Malformed slot. | `time` must match `HH:mm`; `dayOfWeek` must be 0 to 6. |

The `409` body:

```json
{
  "error": "This time slot is already taken in this queue. Choose a different time, send queuedFromProfile without scheduledFor to let the queue assign the next open slot, or send queueId: null to schedule this post outside the queue.",
  "type": "invalid_request_error",
  "code": "queue_slot_conflict",
  "param": "scheduledFor"
}
```

The [queue endpoints](/queue/list-queue-slots) list every response.

## Related

- [Create post](/posts/create-post): every field, including `queueId`.
- [Queue endpoints](/queue/list-queue-slots): list, update and delete slots.
- [Post lifecycle](/guides/post-lifecycle): what `scheduled` means and which webhook fires next.
- [Timezones](/guides/timezones): how `timezone` is read.