# API keys and OAuth

> Create keys, pick scopes, use OAuth for apps and assistants, and rotate or revoke access.

Source: https://usewrit.app/docs/authentication/

Every call to Writ Cloud's API carries a credential. Your own scripts use an API key; apps that act for
other Writ users, and AI assistants, use OAuth. Both are limited to the scopes they were given.

## Create an API key

-

1. **Open Developers → API Keys**

Click **Create API Key**. Only an organization's Owners and Admins can create, change or delete keys.

-

2. **Name it and pick an expiration**

Give it a **Key name** that says where it is used, such as *Reporting script*. For **Expiration**, choose 30 days, 90 days (the default), 1 year or **No expiry**.

-

3. **Choose an access level**

**Read-only** sees everything and changes nothing. **Run automations** (the default) also
runs workflows, harvests, page reads and monitor checks. **Full access** can do everything,
including delete. **Custom** lets you **Choose each permission**, and limit workflows,
monitors or datasets to **Only specific items**.

-

4. **Decide on AI tools**

**Allow AI tools** lets the key use Writ's AI features: AI recording, site discovery and auto-repair.
It is on by default for **Full access** only.

-

5. **Copy the key**

Click **Create key**. The key starts with `wt_` and is shown only once, right after you create it. Store it in a password manager or your app's secret store.

> **note:** Writ emails you when a key is created or deleted. You cannot turn this email off, so a key made without you is noticed.

## Use a key

Send the key in the `Authorization` header of every request. Keep it in an environment
variable such as `WRIT_API_KEY`, never in code you commit or in a web page.

```bash
curl "https://api.usewrit.app/api/runs?limit=5" \
  -H "Authorization: Bearer $WRIT_API_KEY"
```

```python
import os, requests

res = requests.get(
    "https://api.usewrit.app/api/runs",
    headers={"Authorization": f"Bearer {os.environ['WRIT_API_KEY']}"},
    params={"limit": 5},
)
res.raise_for_status()
print(res.json())
```

```typescript
const res = await fetch("https://api.usewrit.app/api/runs?limit=5", {
  headers: {
    Authorization: `Bearer ${process.env.WRIT_API_KEY}`,
  },
});
console.log(await res.json());
```

A missing, wrong or expired key answers `401`. Every call made with your keys counts against your plan's calls per minute: see [Errors and limits](https://usewrit.app/docs/errors).

## Scopes

A scope is a resource and an action, written `resource:action`, such as `workflows:execute`. The four actions are read (view), write (create and change), execute (run) and delete. Not every
resource has all four.

| Resource | In the key editor | Covers | Actions |
| --- | --- | --- | --- |
| workflows | Workflows | Your APIs and workflows, and running them | read, write, execute, delete |
| runs | Runs | Run history, results and live run state | read |
| monitors | Monitors | Monitors, what they watch and the changes they find | read, write, execute, delete |
| datasets | Datasets | Collected data, search and exports | read, delete |
| crawl | Crawls | Harvests and saved harvests | read, execute, delete |
| scrape | Scrape & map | One-off page reads and site maps | execute |
| files | Files | Files you upload and files runs produce | read, write, delete |
| personas | Personas | Personas and their sign-in sessions | read, write, delete |
| secrets | Secrets | Vault secrets: names and details, never the values | read, write, delete |
| agents | Agents | Your machines and fleet agents | read, write, execute |
| devices | Devices | Linked Writ desktops and the workflows and personas they offer | read, write |
| triggers | Triggers & webhooks | Automations and webhooks, and firing them | read, write, execute, delete |
| streaming | Streaming | Live sessions and their OpenAI-compatible calls | read, execute, delete |
| recorder | Recorder | Live recording sessions | read, execute |
| mcp | MCP | The MCP server and the tools you publish on it | read, write, execute, delete |
| marketplace | Marketplace | Browsing and installing ready-made APIs | read, write |
| account | Account | Account, plan and usage, read-only | read |
| transfer | Import & export | Packing the whole account into a file, and importing one | read, write |

The access levels are fixed sets of these scopes:

| Access level | Name in the API | Scopes |
| --- | --- | --- |
| Read-only | read_only | Every read scope except Import & export. |
| Run automations | run | Every read and execute scope except Import & export. |
| Full access | full | Every scope, including delete and Import & export. |

Access is denied by default. A route that is not open to keys answers `403`, whatever the key
holds: managing keys, billing, team members and settings are never reachable with a key. A route the key
lacks the scope for answers `403` and names the missing scope.

```http
HTTP/1.1 403 Forbidden

{ "detail": "API key is missing the 'datasets:read' scope" }
```

```http
HTTP/1.1 403 Forbidden

{ "detail": "This endpoint cannot be called with an API key" }
```

Each operation in the [REST API reference](https://usewrit.app/docs/api) names the scope it needs.

## OAuth for apps

Use OAuth when your app acts for Writ users other than you, or when a platform such as Zapier, Make or
n8n connects to Writ. Each user approves your app on a Writ consent page and can revoke it later. There
are no native Zapier, Make or n8n apps: they connect with OAuth and plain HTTP calls.

-

6. **Create credentials**

In **Integrations**, open the **OAuth access** tab and click **Create an OAuth application**, or pick Zapier, Make, n8n or **Your own application** in the **Catalogue**. Fill in **Name**, **OAuth redirect URLs** (one per line) and the **Permissions** the app may ask for, then click **Create credentials**.

-

7. **Keep the secret**

Writ shows the **Client ID**, the **Client Secret** and the OAuth endpoints. The secret is shown once. Redirect URLs must use HTTPS, except `http://127.0.0.1` or `localhost` for apps on the user's computer.

-

8. **Send the user to the consent page**

Redirect the user to the authorization endpoint with your scopes, separated by spaces. Without a `scope`, the app gets all the permissions you chose. Use PKCE with `S256`: Writ requires it for
apps without a secret and accepts it from every app.

```
https://api.usewrit.app/api/oauth/authorize
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &redirect_uri=https%3A%2F%2Fyour-app.example.com%2Fcallback
  &scope=workflows%3Aread%20workflows%3Aexecute%20datasets%3Aread
  &state=RANDOM_STATE
  &code_challenge=BASE64URL_SHA256_OF_VERIFIER
  &code_challenge_method=S256
```

-

9. **Exchange the code for tokens**

Writ redirects back with `code` and your `state`. Exchange the code within 10 minutes; it works once.

```bash
curl -X POST https://api.usewrit.app/api/oauth/token \
  -d grant_type=authorization_code \
  -d code="$CODE" \
  -d redirect_uri=https://your-app.example.com/callback \
  -d client_id="$WRIT_CLIENT_ID" \
  -d client_secret="$WRIT_CLIENT_SECRET" \
  -d code_verifier="$CODE_VERIFIER"
```

```python
import os, requests

res = requests.post(
    "https://api.usewrit.app/api/oauth/token",
    data={
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": "https://your-app.example.com/callback",
        "client_id": os.environ["WRIT_CLIENT_ID"],
        "client_secret": os.environ["WRIT_CLIENT_SECRET"],
        "code_verifier": code_verifier,
    },
)
res.raise_for_status()
tokens = res.json()  # access_token (wto_...), refresh_token (wtr_...), expires_in
```

```typescript
const res = await fetch("https://api.usewrit.app/api/oauth/token", {
  method: "POST",
  body: new URLSearchParams({
    grant_type: "authorization_code",
    code,
    redirect_uri: "https://your-app.example.com/callback",
    client_id: process.env.WRIT_CLIENT_ID!,
    client_secret: process.env.WRIT_CLIENT_SECRET!,
    code_verifier: codeVerifier,
  }),
});
const tokens = await res.json(); // access_token (wto_...), refresh_token (wtr_...), expires_in
```

```json
{
  "access_token": "wto_...",
  "token_type": "bearer",
  "expires_in": 3600,
  "refresh_token": "wtr_...",
  "scope": "workflows:read workflows:execute datasets:read"
}
```

Call the API with `Authorization: Bearer wto_...`. A token reaches only the routes its scopes
cover, exactly like a key. A missing scope answers `403` with a `WWW-Authenticate: Bearer error="insufficient_scope"` header that names it. Calls with tokens count against the user's plan's
calls per minute.

### Refresh and revoke tokens

Access tokens expire after `expires_in` seconds. Get a new pair with the refresh token. Each refresh
returns a new refresh token and retires the old one; sending a retired refresh token again is refused,
so always store the latest.

```bash
curl -X POST https://api.usewrit.app/api/oauth/token \
  -d grant_type=refresh_token \
  -d refresh_token="$WRIT_REFRESH_TOKEN" \
  -d client_id="$WRIT_CLIENT_ID" \
  -d client_secret="$WRIT_CLIENT_SECRET"
```

```python
import os, requests

res = requests.post(
    "https://api.usewrit.app/api/oauth/token",
    data={
        "grant_type": "refresh_token",
        "refresh_token": stored_refresh_token,
        "client_id": os.environ["WRIT_CLIENT_ID"],
        "client_secret": os.environ["WRIT_CLIENT_SECRET"],
    },
)
res.raise_for_status()
tokens = res.json()
stored_refresh_token = tokens["refresh_token"]  # the old one no longer works
```

```typescript
const res = await fetch("https://api.usewrit.app/api/oauth/token", {
  method: "POST",
  body: new URLSearchParams({
    grant_type: "refresh_token",
    refresh_token: storedRefreshToken,
    client_id: process.env.WRIT_CLIENT_ID!,
    client_secret: process.env.WRIT_CLIENT_SECRET!,
  }),
});
const tokens = await res.json();
storedRefreshToken = tokens.refresh_token; // the old one no longer works
```

To sign a user out of your app, revoke the refresh token or the access token. The endpoint answers `200` either way.

```bash
curl -X POST https://api.usewrit.app/api/oauth/revoke \
  -d token="$WRIT_REFRESH_TOKEN" \
  -d token_type_hint=refresh_token \
  -d client_id="$WRIT_CLIENT_ID" \
  -d client_secret="$WRIT_CLIENT_SECRET"
```

### Endpoints and discovery

| Purpose | URL |
| --- | --- |
| Authorization | https://api.usewrit.app/api/oauth/authorize |
| Token | https://api.usewrit.app/api/oauth/token |
| Revocation | https://api.usewrit.app/api/oauth/revoke |
| Client registration | https://api.usewrit.app/api/oauth/register |
| Server metadata | https://api.usewrit.app/.well-known/oauth-authorization-server |
| Resource metadata (MCP) | https://api.usewrit.app/.well-known/oauth-protected-resource |

```json
{
  "issuer": "https://api.usewrit.app",
  "authorization_endpoint": "https://api.usewrit.app/api/oauth/authorize",
  "token_endpoint": "https://api.usewrit.app/api/oauth/token",
  "revocation_endpoint": "https://api.usewrit.app/api/oauth/revoke",
  "registration_endpoint": "https://api.usewrit.app/api/oauth/register",
  "response_types_supported": ["code"],
  "grant_types_supported": ["authorization_code", "refresh_token"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["client_secret_post", "client_secret_basic", "none"],
  "scopes_supported": ["workflows:read", "workflows:write", "..."]
}
```

### Register a client from code

Clients can register themselves with dynamic client registration, with no step in the app. Writ's MCP
clients do this. A registered client must use PKCE, and it can only ask for the scopes Writ's MCP tools
need.

```bash
curl -X POST https://api.usewrit.app/api/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My MCP client",
    "redirect_uris": ["http://127.0.0.1:33418/callback"],
    "token_endpoint_auth_method": "none"
  }'
```

```json
{
  "client_id": "wmc_...",
  "client_id_issued_at": 1761926400,
  "client_name": "My MCP client",
  "redirect_uris": ["http://127.0.0.1:33418/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "scope": "mcp:read mcp:execute workflows:read workflows:write workflows:execute ..."
}
```

## AI assistants and Writ Desktop

### Assistants sign in with OAuth

When you add `https://api.usewrit.app/mcp` to Claude, ChatGPT, Cursor or another MCP client,
the client finds Writ's OAuth server, registers, and opens a Writ consent page. You approve, and the
client gets a token. You never paste a key. Setup for each client: [Connect an AI assistant](https://usewrit.app/docs/mcp).

An assistant connected this way can read and run your workflows and data, read pages, harvest sites,
sign in with your personas, manage monitors and automations, use your linked desktops and search the
marketplace. It can never delete anything, read your vault secrets or files, manage your machines,
export the account, or install marketplace listings.

> **tip:** To cut off an assistant or app, open Integrations → OAuth access, select it, and click Revoke on a connection or Revoke all access.

### Writ Desktop

Linking Writ Desktop to your account uses the OAuth device flow: Desktop shows a code, and you approve
it in your browser (**Connect a cloud account** → **Open the approval page**). The device
flow is reserved for Writ's own apps; your apps use the authorization code flow above. See [Link Desktop to Writ Cloud](https://usewrit.app/docs/uplink).

Writ Desktop's own API on your computer uses separate local keys that start with `wlk_`, made in **MCP & APIs → API Keys** in the app. Cloud keys do not work there, and local keys do not
work on Writ Cloud. See [Desktop local API](https://usewrit.app/docs/local-api).

## Rotate and revoke

To rotate a key, create a new one, put it where the old one was used, then delete the old one in **Developers → API Keys** with **Delete key**. Deleting cannot be undone, and any
integration using the key stops working at once. To change what a key may do without replacing it, use **Edit permissions**.

Good habits for keys:

- One key per app or script, named after it, so you can revoke one without breaking the others.
- The smallest access level that works: **Read-only** for dashboards, **Run automations** for scripts that start runs.
- Limit keys to **Only specific items** when a script uses one workflow or one dataset.
- An expiration on every key you hand to someone else.
- Keys in environment variables or a secret store, never in a repository, a front-end bundle or a chat.

Writ runs on your own accounts, with your own credentials and data, on sites you are authorized to use.

## Next

-
- [REST API reference](https://usewrit.app/docs/api): Every operation, with the scope it needs.
-
- [Errors and limits](https://usewrit.app/docs/errors): What 401, 403 and 429 mean and what to do.
-
- [Connect an AI assistant](https://usewrit.app/docs/mcp): Add Writ to Claude, ChatGPT, Cursor and others.
-
- [Desktop local API](https://usewrit.app/docs/local-api): Local keys and endpoints on your computer.
