# Docswrite API authentication & agent auth (auth.md)

> How AI agents and servers authenticate to the Docswrite publishing API and MCP server. Developer hub: https://docswrite.com/developers

Docswrite supports two credentials: **OAuth 2.1 access tokens** for agents acting for a person (MCP clients, assistants, IDE agents), and **org API keys** for your own servers and automations. Both are sent as `Authorization: Bearer`.

## Audience

AI agents, MCP clients and backend services that publish content to the blogs, CMSs and social accounts a Docswrite workspace has connected.

## Pick a method

- **OAuth 2.1 (recommended for agents)**: authorization code with PKCE (S256), dynamic client registration, refresh tokens. The person approves your client once on Docswrite's consent screen (https://docswrite.com/oauth/consent) and picks the workspace; the token acts for that person in that workspace only, with the scopes they approved. Access tokens last 1 hour; refresh tokens rotate on every use (30 days each). MCP clients do this automatically when they connect to https://api.docswrite.com/mcp.
- **Org API key**: `dw_live_` + 32 characters, created by a workspace admin in the dashboard (Settings > API Keys, https://docswrite.com/dashboard/api-keys). One key reaches every connection of the workspace. Shown once; stored hashed.
- **Per-site token (legacy)**: a token for a single WordPress connection, sent as `x-access-token`. Kept working for existing integrations.

## Discover

- Protected resource metadata (RFC 9728), MCP server: https://api.docswrite.com/.well-known/oauth-protected-resource/mcp (copy: https://docswrite.com/.well-known/oauth-protected-resource/mcp)
- Protected resource metadata (RFC 9728), REST API: https://api.docswrite.com/.well-known/oauth-protected-resource (copy: https://docswrite.com/.well-known/oauth-protected-resource)
- Authorization server metadata (RFC 8414): https://api.docswrite.com/.well-known/oauth-authorization-server (copy: https://docswrite.com/.well-known/oauth-authorization-server). Issuer: `https://api.docswrite.com`.
- OpenAPI: https://api.docswrite.com/openapi.json
- API catalog (RFC 9727): https://docswrite.com/.well-known/api-catalog
- MCP server card: https://docswrite.com/.well-known/mcp/server-card.json

An unauthenticated call to the MCP server returns `401` with `WWW-Authenticate: Bearer realm="docswrite", resource_metadata="https://api.docswrite.com/.well-known/oauth-protected-resource/mcp"`, so an agent can discover the rest from one request.

## Register

OAuth clients register themselves (RFC 7591):

```http
POST https://api.docswrite.com/oauth/register
Content-Type: application/json

{"client_name": "My Agent", "redirect_uris": ["http://127.0.0.1:33418/callback"], "grant_types": ["authorization_code", "refresh_token"], "token_endpoint_auth_method": "none"}
```

Redirect URIs: https (any host), http only for localhost / 127.0.0.1 / [::1], or a private-use scheme (`cursor://…`). Without `token_endpoint_auth_method` the client is public (PKCE only); `client_secret_post` / `client_secret_basic` clients get a secret once.

Then send the person to:

```
https://api.docswrite.com/oauth/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=connections:read%20posts:read%20posts:write&state=STATE&code_challenge=CHALLENGE&code_challenge_method=S256&resource=https%3A%2F%2Fapi.docswrite.com%2Fmcp
```

`resource` is `https://api.docswrite.com/mcp` for the MCP server or `https://api.docswrite.com` for the REST API. They sign in to Docswrite (Google), see your client name, redirect host and the scopes, pick a workspace and approve (or deny: you get `error=access_denied`). Responses carry `iss`. Exchange the code (single use, 5 minutes) at https://api.docswrite.com/oauth/token with the PKCE verifier and the exact `redirect_uri`.

Docswrite does not issue anonymous agent credentials: every token and key belongs to a person in a workspace, so publishing stays tied to an account and its plan.

### Scopes

- `connections:read`: See your connected sites and social accounts
- `posts:read`: See your posts and their publishing status
- `posts:write`: Create, publish, schedule and update posts on your connections
- `account:read`: See your account, organization and plan, and get upgrade links

No `scope` = all four. `offline_access` is accepted and ignored (a refresh token is always issued). Org API keys carry every scope.

## Use the credential

Base URL: https://api.docswrite.com

```
Authorization: Bearer ACCESS_TOKEN_OR_dw_live_KEY
```

```bash
curl -s https://api.docswrite.com/api/connections \
  -H "Authorization: Bearer dw_live_YOUR_KEY"

curl -s -X POST https://api.docswrite.com/api/v1/posts \
  -H "Authorization: Bearer dw_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 6f1c2a9e-post-42" \
  -d '{"connection_ids": [123, 456], "title": "Hello", "content": "# Hello\n\nWritten by an agent.", "status": "draft"}'
```

An org key may also be sent as `x-api-key`. Never put an org key in a URL (`?token=dw_live_...` is rejected with `400 API_KEY_IN_URL`).

## Rate limits

Org-key calls are limited to 60 requests per minute per key; OAuth calls to 120 per minute per grant. Every response carries `RateLimit-Limit`, `RateLimit-Remaining` and `RateLimit-Reset`; over the limit you get `429 RATE_LIMITED` with `Retry-After`.

Publishing is retry-safe: send `Idempotency-Key` on `POST /api/v1/posts`, `POST /api/posts` and `POST /api/export` (MCP: `idempotency_key` on `publish_post`). A retry with the same key and body replays the first response (`Idempotent-Replayed: true`) for 24 hours.

## Errors

Errors are JSON: `{ "error": true, "code": "...", "message": "..." }`.

- `400 CONNECTION_REQUIRED`: name the target with `connection_id`.
- `400 API_KEY_IN_URL`: send the key in a header.
- `401 INVALID_API_KEY`, `API_KEY_REVOKED`, `API_KEY_EXPIRED`, `invalid_token`: missing or bad credential; `WWW-Authenticate` points at the metadata.
- `403 API_KEY_OWNER_INACTIVE`: the admin who created the key left the workspace; regenerate it.
- `403 INSUFFICIENT_SCOPE`: the token lacks a scope; ask the person to reconnect and approve it.
- `402 PAYMENT_REQUIRED`: the plan is used up; `billing_url` / MCP `upgrade_plan` returns a Stripe checkout link (never charges by itself).
- `404 CONNECTION_NOT_FOUND`: the connection is not in this workspace.
- `422 NOT_PUBLISHED`: no destination accepted the post; see `results`.
- `409 IDEMPOTENCY_IN_PROGRESS`, `422 IDEMPOTENCY_KEY_REUSED`.
- `429 RATE_LIMITED`: wait for `Retry-After`.

## Revocation

- OAuth: revoke a token at https://api.docswrite.com/oauth/revoke (RFC 7009; a refresh token revokes the whole grant), or the person removes your app under Settings > Connected apps (https://docswrite.com/dashboard/settings#connected-apps), which revokes every token of that grant.
- API keys: revoke or regenerate in Settings > API Keys; revoked keys stop working immediately.

Questions: team@docswrite.com
