# auth.md

How an agent authenticates to the Cargo Marshal public REST API at `https://www.cargomarshal.com/api/v1` and to the product MCP server at `https://www.cargomarshal.com/mcp`. Both take a Bearer token of one of two kinds: an OAuth access token, for a client that acts for a person, or an organization API key that a person creates and hands to the agent.

## Discover

Any request without a valid token answers:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", resource_metadata="https://www.cargomarshal.com/.well-known/oauth-protected-resource/api/v1"
```

1. `resource_metadata` points at the API's protected resource metadata (RFC 9728), which also lives at https://www.cargomarshal.com/.well-known/oauth-protected-resource. It names the `resource` `https://www.cargomarshal.com/api/v1`, the `header` Bearer method, the `read` and `write` scopes, this file, and `authorization_servers: ["https://www.cargomarshal.com/api/auth"]`.
2. The authorization server's metadata (RFC 8414) is at https://www.cargomarshal.com/.well-known/oauth-authorization-server/api/auth, and also at https://www.cargomarshal.com/.well-known/oauth-authorization-server. It names the `authorization_endpoint`, the `token_endpoint`, the `registration_endpoint` `https://www.cargomarshal.com/api/auth/oauth2/register`, the `revocation_endpoint`, and `client_id_metadata_document_supported: true`.
3. The OpenAPI document at https://www.cargomarshal.com/api/v1/spec.json declares the `bearerAuth` and `oauth2` schemes and the scope each operation needs.

## Pick a method

- **OAuth**, when the agent acts for a person who can sign in. The agent registers itself and the person consents in the browser.
- **An organization API key**, when no person is present at run time. A person creates the key and hands it to the agent.

`identity_assertion`, `service_auth`, and `anonymous` registration are not supported. Both methods need the Enterprise plan (the `apiAccess` feature). Other plans receive `403 FORBIDDEN` with `data.reason: "plan_required"`.

## Register

### OAuth client

Register with a Client ID Metadata Document: use its HTTPS URL as the `client_id`, and the server fetches it on the first authorization request. Or register by dynamic client registration (RFC 7591):

```http
POST https://www.cargomarshal.com/api/auth/oauth2/register
Content-Type: application/json

{"redirect_uris": ["http://127.0.0.1:53682/callback"], "token_endpoint_auth_method": "none", "client_name": "My agent"}
```

The response carries the `client_id`. A dynamically registered client that no person consents to within 24 hours is deleted.

### API key

A person registers the key. The agent cannot. Only organization owners and admins can create, rotate, or revoke keys.

1. A person signs in at https://www.cargomarshal.com/dashboard and opens **Settings, API keys**.
2. They choose a scope. **Read only** holds the `read` scope and can call GET routes. **Read & write** holds `read` and `write` and can also create and change data.
3. They can set an expiry of 30 days, 90 days, or one year. Without one the key never expires.
4. The secret starts with `sc_live_` and is shown once. Only a hash is stored, so a lost key must be replaced.

There are no sandbox or test keys. Every request acts on the real organization.

## Claim ceremony

OAuth: send the person's browser to the `authorization_endpoint` `https://www.cargomarshal.com/api/auth/oauth2/authorize` with `response_type=code`, the `client_id`, the `redirect_uri`, PKCE (`code_challenge_method=S256`), `scope=read write offline_access`, and `resource=https://www.cargomarshal.com/api/v1`. The person signs in, chooses one organization, and consents. Only roles that can edit Projects can grant `write`. The browser returns to the `redirect_uri` with a `code` and `iss`.

API key: not supported. The person hands the key to the agent through a secret store, not through chat or source control.

## Exchange the assertion

OAuth: exchange the code at the `token_endpoint` `https://www.cargomarshal.com/api/auth/oauth2/token`:

```http
POST https://www.cargomarshal.com/api/auth/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=...&redirect_uri=...&client_id=...&code_verifier=...&resource=https://www.cargomarshal.com/api/v1
```

The access token is a Bearer JWT whose `aud` is `https://www.cargomarshal.com/api/v1`. It lasts 10 minutes. Refresh it with `grant_type=refresh_token`, the refresh token, and the same `resource`. DPoP is not supported.

API key: not supported. The API key is the Bearer token. There is no token endpoint and no refresh token.

## Use the access token

Send the token on every request:

```http
GET https://www.cargomarshal.com/api/v1/libraries/equipment
Authorization: Bearer sc_live_...
```

Write routes also take an `Idempotency-Key` header, so a retry does not create a second Project.

An access token works only for the `resource` it was issued for. A token for `https://www.cargomarshal.com/mcp` is refused by `/api/v1` with `401`, and a token for `/api/v1` is refused by `/mcp`. To call both, ask for both: send `resource` twice in the authorization and token requests.

Rate limits:

- 300 requests per minute per client IP, before authentication.
- 120 requests per minute per API key, and per OAuth connection (one client for one person).
- 30 Calculate enqueues per 10 minutes per API key or OAuth connection.
- 10 Calculates queued or running at once per organization.

Every response reports the limits it counted against in the `RateLimit-Policy` and `RateLimit` headers (IETF httpapi RateLimit header fields), for example `RateLimit-Policy: "requests";q=120;w=60` and `RateLimit: "requests";r=119;t=60`. `q` is the quota per `w` seconds, `r` what is left, and `t` the seconds until the counter has room again. A `401` for an unknown key reports only the per-IP limit. `/mcp` always reports the per-IP limit and adds the per-key limits only when the response is sent after the tool call ran, which older MCP protocol versions do not do. A `429` also sends `Retry-After`.

## Errors

Every error uses the API's standard body with `code` and `status`.

| Status | `code` | Cause | What to do |
| --- | --- | --- | --- |
| 401 | `UNAUTHORIZED` | The token is missing, malformed, expired, issued for another `resource`, or unknown. A key is also refused once revoked or once its creator no longer qualifies, and an OAuth token once the person leaves the organization. The response never says which. | OAuth: refresh the token, and if that fails, authorize again. API key: do not retry with the same key; ask a person to check it under **Settings, API keys**. |
| 403 | `FORBIDDEN` with `data.reason: "insufficient_scope"` | A read-only key, or a connection without `write`, called a `write` route. An OAuth connection also loses `write` when the person's role can no longer edit Projects. | Ask a person for a Read & write key, or authorize again with `write` as a person who can edit Projects. |
| 403 | `FORBIDDEN` with `data.reason: "plan_required"` | The organization is not on the Enterprise plan, the only plan with API access. | Ask a person to move to the Enterprise plan. |
| 429 | `TOO_MANY_REQUESTS` | A rate limit was hit. | Wait the `Retry-After` seconds, then retry. |
| 503 | `SERVICE_UNAVAILABLE` | The rate limiter is unavailable. | Retry with backoff. |

## Revocation

OAuth: revoke a refresh token at the `revocation_endpoint` `https://www.cargomarshal.com/api/auth/oauth2/revoke`. An access token keeps working until it expires, at most 10 minutes. A person can also disconnect the app in the dashboard under **Settings, Connected apps**, and members allowed to manage connected apps can disconnect anyone's.

API key: an owner or admin revokes or rotates a key under **Settings, API keys**. The old secret stops working at once. A key also stops working when its creator leaves the organization or is demoted below admin. There is no revocation endpoint for keys.

## MCP server

The product MCP server at `https://www.cargomarshal.com/mcp` speaks Streamable HTTP and takes the same two kinds of Bearer token. A request without one answers `401` with `WWW-Authenticate: Bearer realm="api", resource_metadata="https://www.cargomarshal.com/.well-known/oauth-protected-resource/mcp"`. That metadata names the same authorization server `https://www.cargomarshal.com/api/auth`, whose metadata is at https://www.cargomarshal.com/.well-known/oauth-authorization-server/api/auth.

Register and authorize as above, with `resource=https://www.cargomarshal.com/mcp`. A Read only key or a connection without `write` can call the read tools, and write tools need `write`. A token or key of an organization without the Enterprise plan gets `403` with `error: "access_denied"` and `reason: "plan_required"`. A write tool called without `write` returns an `insufficient_scope` error. The rate limits above apply per key and per connection. Setup: https://docs.cargomarshal.com/overview/connect-an-agent. Server card: https://www.cargomarshal.com/.well-known/mcp/server-card.json

## More

- OpenAPI document: https://www.cargomarshal.com/api/v1/spec.json
- API reference: https://www.cargomarshal.com/api/v1
- Authentication details: https://docs.cargomarshal.com/overview/persistence-api
- Versioning and deprecation policy: https://docs.cargomarshal.com/overview/api-deprecation-policy
- Quickstart: https://docs.cargomarshal.com/guides/api-quickstart
- API catalog: https://www.cargomarshal.com/.well-known/api-catalog
