What you’ll learn
- Retrieve access tokens for a Google social connection.
- Integrate with an AI agent to call Google APIs.
Before you start
Before you start
Before getting started, make sure you have completed the following steps:
1
Create an Auth0 Account
To continue with this quickstart, you need to have an Auth0 account.
2
Create an Auth0 Application
Go to your Auth0 Dashboard to create a new Auth0 Application.
- Navigate to Applications > Applications in the left sidebar.
- Click the Create Application button in the top right.
- In the pop-up select Regular Web Applications and click Create.
- Once the Application is created, switch to the Settings tab.
- Scroll down to the Application URIs section.
- Set Allowed Callback URLs as:
http://localhost:3000/auth/callback - Set Allowed Logout URLs as:
http://localhost:3000 - Click Save in the bottom right to save your changes.
3
OpenAI Platform
Set up an OpenAI account and API key.
Next.js
FastAPI
Getting started using AI
To get started quickly:Clone auth0-samples/auth0-ai-samples and navigate to
Then, integrate Auth0 AI docs into your preferred AI tool:auth0-ai-samples/call-apis-on-users-behalf/others-api/langchain-next-js directory.- VS Code
- Cursor
- Claude Code
- Claude
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.
Continue Learning
Call Your APIs on User's Behalf
With Auth0, your AI apps can securely call your APIs on behalf of authenticated users.
Call Other's APIs on User's Behalf
Token Vault lets your AI agent call external APIs on a user’s behalf.
How-Tos
Check Google Calendar Availability
Use OpenAI, NextJS, and the Auth0-AI SDKs to check your Google Calendar availability.
List GitHub Repositories
Use OpenAI, NextJS, and the Auth0-AI SDKs to list your GitHub repositores
List Slack Channels
Use OpenAI, NextJS, and the Auth0-AI SDKs to list your Slack channels