- Retrieve access tokens for a Google social connection.
- Integrate with an AI agent to call Google APIs.
Pick your tech stack
LangGraph.js + Next.js
AI SDK + Next.js
LangGraph + FastAPI
AI SDK + React SPA
or Follow manual steps
1
Install packages
In the root directory of your project, install the following packages:
@auth0/ai-langchain: Auth0 AI SDK for LangChain built for AI agents powered by LangChain.@langchain/langgraph: For building stateful, multi-actor applications with LLMs.langchain: The LangChain library.@langchain/core: LangChain core libraries.@langchain/openai: OpenAI provider for LangChain.@langchain/community: LangChain community integrations.langgraph-nextjs-api-passthrough: API passthrough for LangGraph.
2
Update the environment file
Add to your environment file the following variables:To get your Auth0 application’s Lastly, set your OpenAI API key or use any provider supported by the AI SDK.
.env.local
AUTH0_DOMAIN, AUTH0_CLIENT_ID, and AUTH0_CLIENT_SECRET, navigate to the Settings tab in the Auth0 Dashboard. You’ll find these values in the Basic Information section at the top.
Copy each value to the matching setting.Next, run this command to generate a random 32 byte value and copy it to the AUTH0_SECRET field:generate random 32 byte value
3
Set up Token Vault
Use the Auth0 AI SDK for LangChain to get an access token for the Google Social Connection using Token Vault:
connection: pass in the name of the connection you want to access.scopes: pass in the scopes to be authorized for this connection.
src/lib/auth0-ai.ts to instantiate the Auth0 AI SDK client:src/lib/auth0-ai.ts
4
Pass credentials to the tools
Create a file Update your chat route, typically at
/src/lib/auth0.ts file with the following code:src/lib/auth0.ts
/src/app/api/chat/[..._path]/route.ts to pass the refreshToken to your LangGraph agent to use the Auth0 AI SDK to get the Google access token from the server.src/app/api/chat/[..._path]/route.ts
5
Calling APIs from a tool
Once the user is authenticated, you can fetch an access token from Token Vault using the Auth0 AI SDK. In this example, we fetch an access token for a Google social connection.
Once you’ve obtained the access token for a connection, you can use it with an AI agent to fetch data during a tool call and provide contextual data in its response.
This example uses
GmailSearch from the @langchain/community tools. This tool will use the access token provided by Token Vault to query for emails.Update your tool call to request an access token, as shown in the following example:src/lib/agent.ts
6
Add step-up authorization
When you try to use the tool, the application requests any additional Google scopes that are required but not yet authorized. This process is called step-up authorization.To implement, install the Auth0 AI Components for Next.js SDK to get the required UI components:Add a new file, Now, update your chat window code to include the Now when step-up authorization is required, the user will see a prompt in the chat window asking them to authorize.
src/components/auth0-ai/FederatedConnections/FederatedConnectionInterruptHandler.tsx, with the following code:src/components/auth0-ai/FederatedConnections/FederatedConnectionInterruptHandler.tsx
FederatedConnectionInterruptHandler component, for example:src/components/chat-window.tsx
7
Test your application
Start your application. If you are already logged in, make sure to log out and log back in using Google. Then, ask your AI agent to fetch emails from your Gmail account!That’s it! You successfully integrated third-party API access using Token Vault into your app.
Next steps
You have successfully added the ability to get access tokens for tool calling to your application. For next steps:- Call your APIs on user’s behalf docs.
- Learn more about Client-initiated account linking.
- Learn more about how Auth0’s Token Vault manages the tokens of supported identity providers.