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
React SPA
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/vercel-ai-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-vercel: Auth0 AI SDK for AI SDK built for AI agents powered by the AI SDK.ai: Core AI SDK module that interacts with various AI model providers.@ai-sdk/openai: OpenAI provider for the AI SDK.@ai-sdk/react: React UI components for the AI SDK.zod: TypeScript-first schema validation library.googleapis: Node.js client for Google APIs that supports authentication and authorization with OAuth 2.0.
2
Update your environment file
Add the following variables to your environment file: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 AI SDK 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.refreshToken: pass in the function to get the refresh token from the current session.
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 at
/src/lib/auth0.ts file with the following code:src/lib/auth0.ts
Use access token to call APIs from a tool
Once the user is authenticated, you can fetch an access token from the 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 social connection, you can use it with an AI agent to fetch data during a tool call and provide contextual data in its response.In this example, we define a tool call,getCalendarEventsTool, that uses the access token with the Google Calendar API to query for calendar events on a specific date.
Update your tool call to request an access token similar to this example:src/lib/tools/google-calendar.ts
5
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/TokenVault/TokenVaultInterruptHandler.tsx with the following code:src/components/auth0-ai/TokenVault/TokenVaultInterruptHandler.tsx
TokenVaultInterruptHandler component, for example:src/components/chat-window.tsx
6
Add the tool to the AI agent
Handle the interrupts from the AI agent and call the tool from your AI app to get calendar events. Update your chat route, typically at
/src/app/api/chat/route.ts, as shown in the following example:src/app/api/chat/route.ts
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 about your Google calendar!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