Skip to main content
Use OAuth when the API you’re connecting to requires user authorization, such as GitHub, Google, Salesforce, and most SaaS APIs. You register an OAuth capability on your worker, deploy, complete the authorization flow via the CLI, and then call accessToken() in your code to get a valid token.

Define an OAuth capability

Call worker.oauth() with your provider’s OAuth 2.0 endpoints and credentials:
src/index.ts
Store your clientId and clientSecret as secrets, not in code:

Configuration options

Deploy and authorize

Setting up OAuth requires multiple steps in a specific order. The worker must be deployed before you can store secrets or start the OAuth flow:
1

Deploy your worker

If you haven’t deployed at least once already, do so first. The first deploy registers the worker with Notion. Your OAuth credentials won’t be available yet (that’s expected).
2

Get your redirect URL

You’ll need this when creating the OAuth app with your provider.
3

Create an OAuth app with your provider

Go to your provider’s developer settings (e.g., GitHub Developer Settings, Google Cloud Console) and create an OAuth app. Add the redirect URL from the previous step as an authorized redirect URI. Copy the client ID and client secret.
4

Store your OAuth credentials

5

Deploy again

Redeploy so the worker picks up the credentials:
6

Start the OAuth flow

This opens a browser window where you authorize the connection. Once complete, the Workers runtime stores the token securely.

Use the token

Call accessToken() on the OAuth capability object to get a valid token. The runtime handles refresh automatically. This works in tools, syncs, and webhooks:
This works the same way in syncs and webhooks:

Test locally

Once you’ve completed the OAuth flow (via ntn workers oauth start), pull your environment to get a fresh access token locally:
This writes a .env file with all your worker’s secrets, including a fresh OAuth access token. The server refreshes the token automatically before returning it, so the token you get is always valid. You can then test your capability locally with the --local flag:
OAuth tokens expire. If you get a 401 from the provider, run ntn workers env pull again to get a refreshed token.

Examples

Google

Salesforce

Some providers (like Salesforce) don’t return expires_in in their token response. Set accessTokenExpireMs to a sensible default (e.g., 3600000 for 1 hour) so the runtime knows when to refresh.

Next steps

Secrets

Store API keys and OAuth credentials securely.

Syncs

Sync external data into Notion databases.

Webhooks

Receive HTTP events from external services.