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 randomstate, keep both in the user’s session, and redirect.
data:execute covers scrape, search, and enrichment calls.
2. Handle the callback
Context.dev redirects to your callback withcode 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.
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 returns401, and save both new tokens. Each refresh returns a new refresh token, and the old one stops working.
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.
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.
6. Disconnect
When a user disconnects Context.dev in your product, revoke the current refresh token, then delete your copy.401 followed by a failed refresh.