Authentication

Every protected `v1` request uses a Workspace API key or an authorized OAuth access token. This page covers the required header shape, workspace scoping behavior, and authentication failure handling.

5 min read

Use Bearer Authentication

Every protected Mailrith API request uses Bearer authentication. Send the credential in the standard Authorization header. Do not send the credential in a query string, form field, custom header, or request body.

Use workspace API keys for direct server-to-server integrations. Use OAuth access tokens only when Mailrith has approved your integration to use delegated authorization and a user has explicitly connected the integration.

Never send Mailrith credentials from browser JavaScript or mobile app code that users can inspect. Keep API keys and OAuth client secrets on a trusted server.

  1. Load the API key or OAuth access token from a server-side secret store.
  2. Attach the credential to every protected request as Authorization: Bearer <credential>.
  3. Send JSON requests with Content-Type: application/json when the endpoint expects a body.
  4. Handle 401 responses as credential problems. Use the guidance in Errors before you retry the request.
  5. Rotate or revoke workspace API keys from API Keys and Authorized Apps when a credential is exposed or no longer has a clear owner.
Request Headers
Authorization: Bearer mrk_example_secret_key
Content-Type: application/json

Understand Workspace Scope

A workspace API key always belongs to one workspace. The key controls which subscribers, tags, forms, landing pages, broadcasts, sequences, automations, and jobs the request can access.

Do not pass a workspace_id in public /v1 requests. The credential already selects the workspace. This prevents an integration from accidentally writing data into a different workspace because it sent the wrong ID.

If you manage several client or brand workspaces, create a separate key for each workspace. Keep the keys in separate environment variables or secret records so the integration cannot mix subscribers from different workspaces by mistake.

Discover OAuth Metadata

Approved OAuth clients should start from /.well-known/oauth-authorization-server. The discovery response tells the client where to send users for authorization, where to exchange authorization codes for tokens, and where to revoke tokens later.

OAuth is for delegated integrations where a user connects Mailrith to another product. A workspace API key is usually simpler for backend jobs that your own team controls.

Build OAuth clients against the discovery document instead of hard-coding endpoint paths. Send exactly one resource parameter at authorization, code exchange, and refresh. Use https://api.mailrith.com/v1 for the public API or https://api.mailrith.com/mcp for the MCP server.

Request the smallest required scope set. Put permissions the user can safely remove in optional_scope. Mailrith binds the code and both token types to the selected resource and rejects a token at the other resource.

Authorization Server Metadata
curl https://api.mailrith.com/.well-known/oauth-authorization-server

Handle Authentication Failures

If the credential is missing, malformed, expired, revoked, or unknown, Mailrith returns 401 with a stable error envelope. Treat this response as an access problem, not as a temporary server failure.

Do not retry invalid_api_key in a tight loop. The same bad key will keep failing. Alert the integration owner, replace or rotate the credential, and then retry with the new credential.

When an integration suddenly starts receiving 401, check the credential. Confirm that the key was not revoked, was copied correctly, has no extra whitespace, is attached to the correct environment, and was created for the expected workspace.

Authentication Error
{
  "error": {
    "type": "authentication_error",
    "code": "invalid_api_key",
    "message": "A valid workspace API key is required."
  }
}

Add Missing Permissions

Mailrith never silently adds permissions to an existing credential. If an app receives insufficient_scope, read credential_type, missing_scopes, replacement_scopes, and recovery from the error response before asking the user to take action.

For OAuth, the connected app must start authorization again because it owns the secure callback state. Keep the current connection in place while the new authorization runs so existing workflows can continue using their current access.

For a workspace API key, create a replacement key with every permission in replacement_scopes, replace the saved key in the calling app, confirm the new key works, and then revoke the old key. This preserves permissions the integration already uses while adding only what is missing.

  1. Read credential_type, missing_scopes, replacement_scopes, and recovery.action from the 403 insufficient_scope response.
  2. If recovery.action is reconnect_oauth, add the missing permissions to the app's next OAuth request and ask the user to reconnect Mailrith from that app.
  3. If recovery.action is replace_api_key, open Settings, choose API Keys, select Create API Key, and include every permission in replacement_scopes.
  4. Replace the old token or key in the calling app.
  5. Confirm the replacement API key can complete the original operation. Then return to Settings, choose API Keys, open the old key's Actions menu, and select Revoke.
  6. Retry the original operation with the replacement credential.

Need Help Shipping an Integration?

Reach the Mailrith team if you need help planning a sync, validating a webhook flow, or troubleshooting a request.

Contact Mailrith

On this page

Jump to the section you need.