# API Authentication

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

All API requests require a bearer credential. Three credentials work, and all are sent in the standard `Authorization` header:

```bash
Authorization: Bearer your_credential_here
```

The [Full REST API](/docs/api-v1.md) (`/api/v1/...`) and the [MCP Server](/docs/mcp.md) (`/api/mcp`) accept the same credentials and enforce the same rules.

### Three Kinds of Credential

| Credential | Format | Scope | Use it for |
|---|---|---|---|
| **Account API key** (recommended) | `nb_u_...` | Every project in the account. The read-only switch blocks every write, including publishing, generation and billing links. Created at [nextblog.ai/agents](https://nextblog.ai/agents), shown once, revocable immediately | Agents, scripts and CI that work across several projects |
| Project API key (legacy) | `nb_...` (legacy bare UUIDs still work) | Exactly one project: its posts, integrations, connections and analytics, plus the owner's plan/credits and Stripe URLs. Shown in the project's **Settings → API & Agents** | Existing integrations and single-project embeds |
| OAuth 2.1 access token | JWT issued by NextBlog's OAuth server (Supabase Auth) | The whole account: every project, project create/delete, account key management, Search Console disconnect | GUI clients that complete a browser consent flow and cannot send custom headers — Claude, ChatGPT, Cursor, VS Code, Claude Code |

An account key and OAuth both reach the whole account. A project key is deliberately bound to one project: an account-level call made with it fails with `403 ACCOUNT_SCOPE_REQUIRED` — `create_project`, `delete_project`, `rotate_api_key` on another project and `disconnect_gsc`. Use an account key or OAuth for those. The full capability matrix is in [Full REST API → Authentication](/docs/api-v1.md#authentication).

### Creating an Account API Key

1. Open [nextblog.ai/agents](https://nextblog.ai/agents) and find **Account API keys**.
2. Choose **Create key**, name it (for example `Hermes agent`) and decide whether it is read-only.
3. Copy the secret (`nb_u_...`) from the one-time dialog into your environment or secret manager.

The secret is shown once and cannot be retrieved later; if you lose it, revoke the key and create another. A read-only key can read every project, but publishing, generation, integration changes and billing links all answer `403 READ_ONLY_KEY`. Revocation is immediate: the key stops working on the next request, unlike an OAuth access token, which stays valid until it expires (about an hour).

### Creating a Project API Key

To get a project's key:

1. Log into your NextBlog dashboard
2. Open your project → **Settings** → **API & Agents**
3. Copy the key, or click **Rotate** to create a new one (the new key is shown once)
4. Keep this key secure and don't share it publicly

Every project has its own key. `/agents` lists all of them (masked), lets you rotate them and shows recent API/MCP activity.

### Using Your API Key

Include your API key in the Authorization header using the Bearer token format:

```bash
Authorization: Bearer nb_u_your_account_api_key
```

Example request against the current API:

```bash
curl -H "Authorization: Bearer nb_u_your_account_api_key" \
    https://nextblog.ai/api/v1/projects/123/posts?limit=10
```

A project key works the same way for its one project:

```bash
curl -H "Authorization: Bearer nb_your_project_api_key" \
    https://nextblog.ai/api/v1/projects/123/posts?limit=10
```

The legacy display endpoints accept the same key:

```bash
curl -H "Authorization: Bearer nb_your_project_api_key" \
    https://nextblog.ai/api/blog/123
```

### Using an OAuth Access Token

Agents obtain OAuth tokens through the browser consent flow described in [MCP Server → Authentication](/docs/mcp.md#authentication); your own server can send the issued JWT the same way:

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

OAuth tokens are account-wide and expire (typically after about an hour); clients refresh them automatically.

### 401 Responses and Discovery

Every unauthenticated request returns `401` with a `WWW-Authenticate` header pointing at the RFC 9728 protected-resource metadata:

```
WWW-Authenticate: Bearer resource_metadata="https://nextblog.ai/.well-known/oauth-protected-resource"
```

`GET https://nextblog.ai/.well-known/oauth-protected-resource` (and the path-suffixed `.well-known/oauth-protected-resource/api/mcp`) is public, CORS-enabled and returns the authorization server, supported bearer methods and scopes. MCP clients use it to discover how to authenticate; no configuration is needed on your side.

### When a Key or Token Stops Working

- **Revoked account key:** it stops working on the next request with `401 INVALID_API_KEY`. Revoke a compromised key at [/agents](https://nextblog.ai/agents) and create a replacement there.
- **Rotated project key:** the old key stops working immediately. Update your environment variables and the client config.
- **Revoked agent grant:** revoke on [/agents](https://nextblog.ai/agents). Already-issued access tokens keep working until they expire (about an hour); after that the client must reconnect and you approve it again.
- **Expired OAuth token:** the client should refresh it automatically. A manual `curl` needs a fresh token.

### Security Best Practices

- Never expose an API key in client-side code
- Use environment variables to store API keys
- Prefer a read-only account key for agents that only need to read
- Rotate project keys and revoke account keys from `/agents`
- Review connected agents and recent activity on `/agents`
- Monitor API usage for suspicious activity
