# Full REST API

> Part of the [NextBlog documentation](https://nextblog.ai/docs.md). Live page: [https://nextblog.ai/docs/api-v1](https://nextblog.ai/docs/api-v1)

`https://nextblog.ai/api/v1` is the complete public API: every action available in the dashboard, implemented once in the same service layer as the [MCP Server](/docs/mcp.md), so the two can never drift. JSON in, JSON out.

- **Base URL:** `https://nextblog.ai/api/v1`
- **OpenAPI 3.1:** [https://nextblog.ai/api/v1/openapi.json](https://nextblog.ai/api/v1/openapi.json) — public, no authentication, `Access-Control-Allow-Origin: *`, cached 5 minutes.
- **Legacy aliases:** `/api/blog/...` and `/api/manage/...` still work unchanged (see [Blog Display API](/docs/endpoints.md) and [Post Management API](/docs/api-manage-posts.md)). New code should use v1.
- **No SDK required:** any HTTP client works. The MCP server exists for agents; v1 is for your own code.

### Authentication

Send a bearer credential on every request:

```bash
Authorization: Bearer nb_u_your_account_api_key
```

An account API key (`nb_u_...`) covers every project and is the default for agents and CI. A project API key (`nb_...`, legacy) still works and is bound to exactly one project. OAuth access tokens are issued to an app/agent through the browser consent flow (see [API Authentication](/docs/authentication.md) for all three). What each credential may do:

| Caller | Account reads | Project reads | Project writes | Create/delete project, rotate keys | Stripe URLs | Generate (spends credits) |
|---|---|---|---|---|---|---|
| Account API key | ✅ | ✅ all owned projects | ✅ | ✅ | ✅ | ✅ |
| OAuth access token | ✅ | ✅ all owned projects | ✅ | ✅ | ✅ | ✅ |
| Project API key (legacy) | Limited (`/me`, `/billing/status`, own project) | ✅ own project only | ✅ own project only | ❌ `403 ACCOUNT_SCOPE_REQUIRED` | ✅ owner's account | ✅ |

Notes:

- A read-only account key refuses every write with `READ_ONLY_KEY` (`403`), including publishing, generation and billing links.
- Account-wide calls over a project key are refused with `ACCOUNT_SCOPE_REQUIRED`, including `POST/DELETE /projects`, `POST /projects/{projectId}/api-key` and `POST /gsc/disconnect`.
- `POST /billing/checkout` and `POST /billing/portal` are allowed for project keys because the URLs act on the key owner's own account. They create a session only — the API never charges a card.
- Every response is scoped to the caller. A post, project or connection owned by somebody else answers `404` (not `403`) so existence is never leaked; a project id outside an API key's project answers `403 PROJECT_SCOPE_VIOLATION`.

### Errors

Every failure uses one envelope:

```json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid request.",
    "details": [{ "path": "title", "message": "Title is required" }]
  }
}
```

| Code | Status | Meaning |
|---|---|---|
| `AUTH_REQUIRED` | 401 | No credential. Includes `WWW-Authenticate` with the discovery URL |
| `INVALID_API_KEY` | 401 | Unknown, revoked, rotated or deleted API key |
| `FORBIDDEN` | 403 | Operation not allowed (e.g. a disallowed browser origin) |
| `PROJECT_SCOPE_VIOLATION` | 403 | The credential does not cover this project |
| `ACCOUNT_SCOPE_REQUIRED` | 403 | The credential is bound to a single project and the action needs account scope — use an account API key or OAuth |
| `READ_ONLY_KEY` | 403 | The account key is read-only; every mutating request is refused |
| `UPGRADE_REQUIRED` | 403 (400 on create) | Plan gating: paid feature on a preview account, project limit reached |
| `NOT_FOUND` | 404 | Resource missing or not owned by the caller |
| `METHOD_NOT_ALLOWED` | 405 | Wrong HTTP method; the response carries `Allow` |
| `CONFLICT` | 409 | Duplicate slug, reused idempotency key, or an active video job |
| `VALIDATION_ERROR` | 400 (413 for oversized bodies) | Request failed validation; `details` lists each issue as `{ path, message }` |
| `INSUFFICIENT_CREDITS` | 402 | Not enough credits; `details` has `needed` and `available` |
| `RATE_LIMITED` | 429 | Limit exceeded; `details` has `limit`, `retryAfter` and `window` |
| `PROVIDER_NOT_CONFIGURED` | 503 | Optional upstream (voices) has no API key in the deployment |
| `UPSTREAM_ERROR` | 502 | Optional upstream failed |
| `INTERNAL_ERROR` | 500 | Unexpected server error; the message never leaks internals |

`CONFIRMATION_REQUIRED` exists only on the MCP surface — HTTP has no confirmation argument; asking for the action is the confirmation. For the legacy display/manage routes, keep using their documented status codes; they call the same services and map errors back to their old responses.

### Pagination

- `limit` — page size, default **50**, maximum **100** (values outside 1–100 are a `400 VALIDATION_ERROR`).
- `offset` — rows to skip, default `0`.
- `GET /projects/{projectId}/posts` also returns `total`, the unpaginated count matching the filters.

```bash
curl -H "Authorization: Bearer nb_..." \
  "https://nextblog.ai/api/v1/projects/123/posts?status=draft&limit=20&offset=40"
```

```json
{ "posts": [ ... ], "total": 346 }
```

### Rate Limits

Limits are per credential (one token = one budget across REST v1 and MCP):

| Policy | Applies to | Limit |
|---|---|---|
| `default` | Every request | **120/minute + 1,000/hour** |
| `generate_post` | `POST /projects/{projectId}/generate` | 10/hour |
| `generate_video` | `POST .../posts/{postId}/generate-video` | 10/hour |
| `billing` | `POST /billing/checkout`, `POST /billing/portal` | 20/hour |

A blocked request returns `429`:

```json
{
  "error": {
    "code": "RATE_LIMITED",
    "message": "Rate limit exceeded (120 requests per minute). Retry in 47 seconds.",
    "details": { "limit": 120, "retryAfter": 47, "window": "minute" }
  }
}
```

with `Retry-After`, `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset`. Successful responses carry the `X-RateLimit-*` headers for the window that will bind first.

### Idempotency

Send an `Idempotency-Key` header on any `POST`, `PATCH` or `DELETE` to make retries safe:

- The first 2xx JSON response is stored for **24 hours**; repeating the same key with the same action replays it, with `X-Idempotent-Replay: true`.
- Reusing a key for a different action returns `409 CONFLICT`.
- 4xx/5xx are not stored, so you can fix a validation error and retry with the same key.
- Keys are per account and must be at most 255 characters. MCP tools accept an `idempotency_key` argument for the same behaviour on `create_project`, `create_post`, `generate_post`, `generate_video` and `create_checkout_link`.

### Content Limits

| Field | Limit |
|---|---|
| `markdown` | 200 KB of UTF-8 |
| `title` | 200 characters |
| `description` | 500 characters |
| List page | `limit` 1–100 |
| Request body | 200 KB of JSON (MCP: 1 MB) |

### Endpoints

All paths below are relative to `https://nextblog.ai/api/v1`. `{projectId}`, `{postId}` and `{connectionId}` are ids from `/projects`, `/posts` and `/connections/*`.

#### Account and billing

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/me` | — | `{ user, auth, scopedProject, plan, credits, projects }` — same snapshot as the `whoami` tool; `auth` names the account key when one is used |
| GET | `/keys` | — | `{ keys }` — account key metadata, never the secret. OAuth or dashboard session only |
| POST | `/keys` | `{ name, readOnly? }` | `{ key, secret }` (201) — `nb_u_...` secret shown once. OAuth or dashboard session only |
| DELETE | `/keys/{keyId}` | — | `204`. Revocation is immediate. OAuth or dashboard session only |
| GET | `/analytics?days=7\|30\|90` | `days` default 30 | Totals, previous-period comparison, zero-filled daily timeline, project stats, top/recent posts |
| GET | `/billing/status` | — | `{ plan, credits, limits, hasExistingPayment, isPreview }` |
| POST | `/billing/checkout` | `{ planId, billedYearly?, isSignUpOffer?, referralCode?, offer?, token? }` | `{ url }` — Stripe Checkout. Never charges |
| POST | `/billing/portal` | — | `{ url }` — Stripe Customer Portal. `404` with `details.reason` when there is no subscription |
| GET | `/voices?provider=all\|elevenlabs\|xai` | — | `{ voices, providers: { elevenlabs: { configured }, xai: { configured } } }` |

#### Projects

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/projects?limit=&offset=` | — | `{ projects }` with `post_count` and `configured` flags |
| POST | `/projects` | `{ name?, url?, description?, extract? }` (`extract` requires a public `url`) | `{ project }` (201). Account key or OAuth |
| GET | `/projects/{projectId}` | — | `{ project }` — secrets replaced by `configured` |
| PATCH | `/projects/{projectId}` | Any editable field: `name`, `url`, `description`, schedule, `language`, `article_length`, subdomain, internal linking, backlinks, CMS credentials, webhook, GSC, video | `{ project }` |
| DELETE | `/projects/{projectId}` | — | `{ deleted: true, id }`. Account key or OAuth; deletes its posts too |
| POST | `/projects/{projectId}/api-key` | — | `{ project, apiKey }` — new key shown once. Account key or OAuth |
| POST | `/projects/{projectId}/generate-description` | `{ url? }` | `{ description }` — not saved; shares the 5/day quota |
| GET | `/projects/{projectId}/backlinks` | — | `{ given, received }` |
| GET | `/projects/{projectId}/generation-runs?instanceId=` | — | `{ runs }` — last 20, newest first |
| POST | `/projects/{projectId}/generate` | — | `{ instanceId, status, blogId? }` — spends credits; poll the runs endpoint |

#### Posts and generation

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/projects/{projectId}/posts?status=&published=&limit=&offset=` | — | `{ posts, total }` — no markdown |
| POST | `/projects/{projectId}/posts` | `{ title, markdown, slug?, description?, cover_image_url?, cover_image_alt?, status? }` | `{ post }` (201) with markdown |
| GET | `/projects/{projectId}/posts/{postId}` | — | `{ post }` — markdown, integration URLs, video status |
| PATCH | `/projects/{projectId}/posts/{postId}` | Any of `title`, `markdown`, `slug`, `description`, `cover_image_url`, `cover_image_alt`, `status`, `published` | `{ post }` |
| DELETE | `/projects/{projectId}/posts/{postId}` | — | `{ deleted: true, id }` |
| POST | `/projects/{projectId}/posts/{postId}/approve` | — | `{ post, integrationUpdates: { success, failed } }`. Paid plan required |
| POST | `/projects/{projectId}/posts/{postId}/decline` | — | `{ post }` |
| POST | `/projects/{projectId}/posts/{postId}/unpublish` | — | `{ post, integrationUpdates: { success, failed } }` |
| POST | `/projects/{projectId}/posts/{postId}/generate-video` | — | `{ post, videoJob }` — spends credits; needs YouTube + xAI voice |
| GET | `/projects/{projectId}/posts/{postId}/video` | — | `{ video }` — job or `null` |

#### Integrations

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/projects/{projectId}/integrations` | — | Provider map and `publishTargets`; credentials never returned |
| PATCH | `/projects/{projectId}/integrations` | Provider-grouped bodies (`{ wordpress: { enabled, url, username, password } }`), flat columns, or `{ integrations: ... }` | Updated provider map |
| POST | `/projects/{projectId}/integrations/{provider}/test` | Optional credential overrides (never echoed) | `{ provider, success, message }` |
| GET | `/projects/{projectId}/integrations/{provider}/connect-url?returnTo=` | `provider`: `shopify`, `youtube`, `google`, `blogger`, `gsc` | `{ provider, url, requiresBrowser: true, next }` — open the URL in a browser |
| POST | `/projects/{projectId}/gsc/test` | `{ siteUrl }` | Search Console verification result |

#### Connections and Search Console

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/connections/youtube` | — | `{ youtube }` — non-secret channel identifiers |
| DELETE | `/connections/youtube/{connectionId}` | — | Disconnect result |
| GET | `/connections/shopify` | — | `{ shopify }` — project-scoped shops |
| DELETE | `/connections/shopify/{connectionId}` | — | Disconnect result |
| GET | `/gsc/status` | — | `{ connected, tokenExpiry }` |
| GET | `/gsc/sites` | — | `{ sites }` — raw Google `siteEntry` objects |
| POST | `/gsc/disconnect` | — | `{ disconnected: true }`. Account key or OAuth; clears GSC on every project |

#### Discovery

| Method | Path | Request | Response |
|---|---|---|---|
| GET | `/openapi.json` | — | The OpenAPI 3.1 document (public, CORS `*`) |

### Examples

Account API key (every project):

```bash
# Who am I and what can this key reach?
curl -H "Authorization: Bearer nb_u_your_account_api_key" \
  https://nextblog.ai/api/v1/me

# Create a draft
curl -X POST https://nextblog.ai/api/v1/projects/123/posts \
  -H "Authorization: Bearer nb_u_your_account_api_key" \
  -H "Content-Type: application/json" \
  -d '{"title":"My post","markdown":"# Hello","status":"draft"}'

# Publish it to every enabled integration
curl -X POST https://nextblog.ai/api/v1/projects/123/posts/456/approve \
  -H "Authorization: Bearer nb_u_your_account_api_key"

# Queue a generation run and poll it
curl -X POST https://nextblog.ai/api/v1/projects/123/generate \
  -H "Authorization: Bearer nb_u_your_account_api_key"
curl -H "Authorization: Bearer nb_u_your_account_api_key" \
  "https://nextblog.ai/api/v1/projects/123/generation-runs?instanceId=<instanceId>"
```

A legacy project API key (`nb_...`) works in the same examples for its one project.

OAuth access token (account-wide):

```bash
curl -H "Authorization: Bearer <oauth-access-token>" \
  https://nextblog.ai/api/v1/projects

curl -X POST https://nextblog.ai/api/v1/projects \
  -H "Authorization: Bearer <oauth-access-token>" \
  -H "Content-Type: application/json" \
  -d '{"name":"My site","url":"https://example.com","extract":true}'
```

Generate a client from the OpenAPI document instead of hand-writing requests:

```bash
npx @hey-api/openapi-ts -i https://nextblog.ai/api/v1/openapi.json -o ./src/client
```

### Migrating from the Legacy APIs

| Legacy | Use instead |
|---|---|
| `GET /api/blog/{projectId}` | `GET /api/v1/projects/{projectId}/posts` (adds pagination, filters, `total`) |
| `GET /api/blog/{projectId}/{slug}` | `GET /api/v1/projects/{projectId}/posts` and match `slug`, or `GET .../posts/{postId}` when you know the id |
| `GET /api/manage/{projectId}/posts` | `GET /api/v1/projects/{projectId}/posts` |
| `POST /api/manage/{projectId}/posts` | `POST /api/v1/projects/{projectId}/posts` |
| `GET/PATCH /api/manage/{projectId}/posts/{postId}` | `GET/PATCH /api/v1/projects/{projectId}/posts/{postId}` |
| — | `DELETE .../posts/{postId}`, `/approve`, `/decline`, `/unpublish`, `/generate-video` are v1-only |

Legacy responses stay as documented on their pages; v1 returns resource objects (`{ project }`, `{ posts, total }`, `{ run }`) and the single error envelope above. Requests to the legacy bases share the same rate-limit bucket and audit log as v1.
