Skip to main content
Use this when your product has a backend and each user should pay for their own Context.dev usage. The user clicks Connect Context.dev once. After that your backend calls the API with their token. You need a Server app from the OAuth apps page, with your callback URL as a redirect URI. Keep the client ID and client secret in server-side environment variables. The examples use Node.js 20 or later with Express 5 and express-session, and assume your own sign-in has already set req.user. Express 5 passes a rejected async handler to your error handler; on Express 4, wrap each handler in try/catch. The flow is the same in any language.

1. Send the user to Context.dev

Create a PKCE code verifier and a random state, keep both in the user’s session, and redirect.
Request only the scopes your product uses. data:execute covers scrape, search, and enrichment calls.

2. Handle the callback

Context.dev redirects to your callback with code and state, or with error=access_denied if the user cancels. Check state before anything else, and clear the pending sign-in only once it matches, so a stray request to your callback can’t cancel a sign-in that is under way. Then exchange the code from your server. The code works once, for five minutes.
Store the tokens with your record of the user, encrypted at rest. Keep them out of browser code, URLs, and logs. organization_id is the workspace the user picked.

3. Call the API

Send the access token the same way you send an API key. The SDKs accept it as the API key.

4. Refresh the access token

Access tokens last one hour. Refresh shortly before expiry, or when the API returns 401, and save both new tokens. Each refresh returns a new refresh token, and the old one stops working.
Only invalid_grant means the refresh token can no longer be used. A 5xx is temporary, and invalid_client means your client ID or secret is wrong, so the sample reports those without ending the connection. If two workers refresh with the same refresh token, the second gets invalid_grant. The sample handles that by reloading the stored tokens and using them when another worker has already saved new ones. It treats the connection as ended only when nothing changed. A lock on the user’s row, so that one refresh runs at a time, avoids the wasted request.
Always save the new refresh token before you use the new access token. A refresh token that is used again more than a minute after it was replaced ends the connection, because it can mean the token was copied. The user then has to reconnect.

5. Handle what can go wrong

A 401 can arrive while the stored token still looks valid, for example right after the user disconnects your app. Wrap your calls so a 401 forces one refresh and one retry.
A refresh token lasts 30 days from its last use, so a user who is inactive for longer has to reconnect.

6. Disconnect

When a user disconnects Context.dev in your product, revoke the current refresh token, then delete your copy.
Delete your copy only after the request succeeds, so a failed request can be retried. Revoking ends that connection, including the tokens held by the user’s other devices. Users can also disconnect from their Connected apps page, which your product sees as a 401 followed by a failed refresh.