Set up connectors and get OAuth tokens and call tools on the fly using Scalekit's Agent Auth
Users ask your app in natural language - “Show me last 5 unread emails” or “Create a calendar event for tomorrow” or “Send an alert to my team”
All of these actions require your app to be able to securely connect to third-party applications like Gmail, Calendar, Slack, Notion etc. and execute actions on behalf of the user. Scalekit handles:
Authorizing your application with 3rd party apps.
Allows you to configure a connection with each 3rd party app using your own credentials or using Scalekit’s credentials (for faster development).
Every connection would maintain token vault allowing you to fetch tokens and use them to make API calls.
Provides a unified API to call tools on behalf of the user. Scalekit provides tools if your preferred connector is not supported.
Manage each user account per connector through Scalekit dashboard or programmatically.
Connected account is a user’s 3rd party account that is registered in your Scalekit environment. This represents the user’s connection to their Gmail account in this case.
In order to execute any tools on behalf of the user, the user first needs to grant authorization to access their gmail account. Scalekit automatically handles the entire OAuth workflow with the Gmail provider, gets the access token, refreshes the access token periodically based on the refresh token etc.
If the user’s access token is expired, Scalekit will automatically refresh the access token using the refresh token. At any time, you can check the authorization status of the connected account and determine if the user needs to re-authorize the connection.
Now that the user has successfully authorized the Gmail connection, you can fetch the latest access token and refresh token for the user’s connected account anytime you want.
Scalekit will keep the token refreshed periodically so that you always have the latest valid access token to make API calls on behalf of the user.
// Security Warning: Never log OAuth tokens in production. Token exposure in logs
// or aggregators creates credential leakage risks. Use safe redaction,
// conditional debug-only logging, or secure secret storage instead.
if(process.env.NODE_ENV==='development'){
console.log('accessToken:',accessToken);
console.log('refreshToken:',refreshToken);
}
Refer to the respective connector’s API documentation for more details on how to use these tokens and consume their API. Scalekit provides tools for most of the connectors, so you can use them to call the API directly.
Now that the user is authenticated and you have the access token, you can execute the Gmail API to fetch the last 5 unread emails from the user’s inbox.
The example below directly calls the Gmail API. You can use the Gmail SDK or any other HTTP client of your choice to make the API calls.