# OAuth for apps

> For apps that act for other brands and creators. Each person signs in to Yap Factory and approves what your app can do. OAuth 2.1 with PKCE, for public clients: no client secrets.

Building for your own account only? An [API key](https://yapfactory.com/developers/quickstart.md) is simpler. MCP clients such as Claude and ChatGPT do all of this for you.

## The flow

1. **Discover** the authorization server from the metadata.
2. **Identify your app** with a Client ID Metadata Document, or register it.
3. **Send the person to authorize** with a PKCE challenge. They sign in and approve.
4. **Swap the code for tokens** at the token endpoint.
5. **Call the API or the MCP server** with the access token. Refresh it when it expires.
6. **Revoke** when the person disconnects in your app.

## Discovery

A call to the MCP server without a token gets `401` with `WWW-Authenticate: Bearer resource_metadata="…"`, which points to the protected resource metadata (RFC 9728). That names the authorization server, whose metadata (RFC 8414) lists every endpoint below.

| What | URL |
| --- | --- |
| Protected resource metadata | `https://yapfactory.com/.well-known/oauth-protected-resource/mcp` (MCP) and `https://yapfactory.com/.well-known/oauth-protected-resource/api/v1` (REST) |
| Authorization server metadata | `https://yapfactory.com/.well-known/oauth-authorization-server` (also at `/.well-known/openid-configuration`) |
| Authorize | `https://yapfactory.com/oauth/authorize` |
| Token | `https://yapfactory.com/oauth/token` |
| Register (DCR) | `https://yapfactory.com/oauth/register` |
| Revoke | `https://yapfactory.com/oauth/revoke` |

```bash
curl https://yapfactory.com/.well-known/oauth-authorization-server
```

## Identify your app

### Client ID Metadata Document (preferred)

Host a small JSON file on your own https domain and use its URL as your `client_id`. We fetch it when someone first connects, and again once a day. Nothing to register, and your name and logo stay up to date.

https://yourapp.com/oauth/client.json:

```json
{
  "client_id": "https://yourapp.com/oauth/client.json",
  "client_name": "Your App",
  "client_uri": "https://yourapp.com",
  "logo_uri": "https://yourapp.com/logo.png",
  "redirect_uris": [
    "https://yourapp.com/oauth/callback"
  ],
  "grant_types": [
    "authorization_code",
    "refresh_token"
  ],
  "token_endpoint_auth_method": "none"
}
```

- `client_id` must equal the file's URL exactly.
- Serve it with status `200`, no redirects, under 20 KB, and answer within 5 seconds.
- Up to 20 `redirect_uris`.

### Dynamic Client Registration

Or register once (RFC 7591) and keep the `client_id` you get back. Clients are always public: no secret is issued, and `token_endpoint_auth_method` is `none`.

```bash
curl -X POST https://yapfactory.com/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "Your App",
    "redirect_uris": ["https://yourapp.com/oauth/callback"],
    "grant_types": ["authorization_code", "refresh_token"]
  }'
```

201 Created:

```json
{
  "client_id": "yfc_8fK2mQ9xLp3VtR7nYc1B4dWz",
  "client_name": "Your App",
  "redirect_uris": [
    "https://yourapp.com/oauth/callback"
  ],
  "grant_types": [
    "authorization_code",
    "refresh_token"
  ],
  "response_types": [
    "code"
  ],
  "token_endpoint_auth_method": "none"
}
```

Send 1 to 10 redirect URIs. Client ids from registration start with `yfc_`.

### Redirect URIs

- `https` anywhere.
- `http` only on loopback (`localhost`, `127.0.0.1`, `[::1]`), for desktop apps and CLIs. Loopback redirects match on any port.
- A native app's own scheme, such as `cursor://` or `vscode://`.
- Never a fragment, or a user name or password in the URL.

## Authorize

Make a random `code_verifier` (43 to 128 characters), hash it with SHA-256 for the `code_challenge`, and send the person's browser here. Only `S256` is accepted.

```text
https://yapfactory.com/oauth/authorize
  ?response_type=code
  &client_id=https%3A%2F%2Fyourapp.com%2Foauth%2Fclient.json
  &redirect_uri=https%3A%2F%2Fyourapp.com%2Foauth%2Fcallback
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256
  &scope=read%20write
  &state=af0ifjsldkj
  &resource=https%3A%2F%2Fyapfactory.com%2Fapi%2Fv1
```

- `resource` (RFC 8707) is `https://yapfactory.com/api/v1` or `https://yapfactory.com/mcp`. One token works for both.
- The person signs in if they need to, sees your app's name, where it sends them back to, and what it asks for. They can untick the second line to give read-only access, so check the `scope` you get back with the tokens.
- The consent screen says the app can draft and publish campaigns, invite creators, answer invites, send messages and schedule posts to their TikTok and Instagram, and that it can never charge their card, pay a creator or cancel a deal.
- If the person says no, we redirect back with `error=access_denied` and your `state`. Anything wrong with the request itself (an unknown `client_id`, a `redirect_uri` that isn't registered, a missing challenge) is shown to the person on our page, and we never redirect.
- We redirect back with `code`, your `state` and `iss` (RFC 9207). Check that `iss` is the issuer from the metadata. Codes last 10 minutes and work once.

## Get tokens

Swap the code at the token endpoint, form-encoded (JSON works too). Send `client_id`; there's no secret.

```bash
curl -X POST https://yapfactory.com/oauth/token \
  -d grant_type=authorization_code \
  -d code=yfo_... \
  -d redirect_uri=https://yourapp.com/oauth/callback \
  -d client_id=https://yourapp.com/oauth/client.json \
  -d code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
```

```json
{
  "access_token": "yfa_...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "yfr_...",
  "scope": "read write"
}
```

Send the access token as `Authorization: Bearer yfa_...` to the REST API or the MCP server.

## Refresh

Access tokens last an hour. Refresh tokens last 30 days and **rotate**: each refresh returns a new pair, and the old refresh token stops working. Store the new one every time.

```bash
curl -X POST https://yapfactory.com/oauth/token \
  -d grant_type=refresh_token \
  -d refresh_token=yfr_... \
  -d client_id=https://yourapp.com/oauth/client.json
```

- A refresh keeps the scope the person approved, and the answer's `scope` says what that is. Asking for more returns `invalid_scope`: send the person through authorize again.
- **Using a code or a refresh token twice ends the connection.** We take it as a sign someone else has a copy. The person connects again.

## Revoke

When someone disconnects in your app, revoke (RFC 7009). Revoking any token of a connection ends the whole connection. The answer is always `200`.

```bash
curl -X POST https://yapfactory.com/oauth/revoke -d token=yfr_...
```

People can also see and disconnect every connection themselves in **Settings → AI agents**. After that your tokens return `401`.

## Errors and limits

The token, register and revoke endpoints answer errors the OAuth way, not with the API's envelope:

```json
{
  "error": "invalid_grant",
  "error_description": "That code is invalid or has expired."
}
```

| `error` | Status | When |
| --- | --- | --- |
| `invalid_grant` | 400 | The code or refresh token is wrong, expired, already used, or was issued to another client; the `redirect_uri` doesn't match; or the PKCE check failed. |
| `invalid_client` | 401 | No `client_id`, one we don't know, or a metadata document we couldn't read. |
| `invalid_scope` | 400 | A scope we don't have, or a refresh asking for more than was approved. |
| `invalid_target` | 400 | A `resource` that isn't the API or the MCP server. |
| `unsupported_grant_type` | 400 | Anything but `authorization_code` and `refresh_token`. |
| `invalid_client_metadata` | 400 | Registration with a bad redirect URI or grant type. |
| `invalid_request` | 400 | A missing or malformed parameter. |
| `temporarily_unavailable` | 429 | Too many calls from one place. Wait a few minutes. |

Limits per caller: 60 authorize requests and 120 token requests every 10 minutes, 60 revokes every 10 minutes, and 30 registrations an hour. Register once and keep the `client_id`; don't register per person.

## Scopes

Ask for the coarse scopes, which is what the consent screen shows, or one resource at a time.

| Scope | Allows |
| --- | --- |
| `read` | Every operation that reads. |
| `write` | Every operation that acts, and every read. Asking for `write` adds `read`. |
| `<resource>:read` | Reads on one resource. |
| `<resource>:write` | Acts on one resource, and reads it. |

Resources: `account`, `deals`, `campaigns`, `creators`, `marketplace`, `posts`. So the full list is `read`, `write`, `account:read`, `account:write`, `deals:read`, `deals:write`, `campaigns:read`, `campaigns:write`, `creators:read`, `creators:write`, `marketplace:read`, `marketplace:write`, `posts:read`, `posts:write`. A missing or empty `scope` means `read write`.

Each [operation's page](https://yapfactory.com/developers/api.md) names the scope it needs. Without it you get `403 insufficient_scope`, with the scope to ask for in `meta.required`.

---

HTML version: https://yapfactory.com/developers/oauth
