What you’ll learn
- Obtain an access token: learn how to authenticate and securely retrieve an access token from Auth0.
- Call an API: use the token to make a request to your API (e.g.: Auth0’s
/userinfoendpoint). - Present user data: pass the retrieved data back through an AI agent for consumption or display.
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
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/your-api/vercel-ai-next-js directory.- VS Code
- Cursor
- Claude Code
- Claude
or Follow manual steps
1
Install dependencies
2
Update the environment file
Copy the
.env.example file to .env.local and update the variables with your Auth0 credentials. You can find your Auth0 domain, client ID, and client secret in the application you created in the Auth0 Dashboard.3
Define a tool to call your API
In this step, you’ll create a AI SDK tool to make the first-party API call. The tool fetches an access token to call the API.In this example, after taking in an Auth0 access token during user login, the tool returns the user profile of the currently logged-in user by calling the /userinfo endpoint.
src/lib/tools/user-info.ts
4
Add the tool to the AI agent
The AI agent processes and runs the user’s request through the AI pipeline, including the tool call. AI SDK simplifies this task with the You need an API Key from OpenAI or another provider to use an LLM. Add that API key to your If you use another provider for your LLM, adjust the variable name in
streamText() method. Update the /src/app/api/chat/route.ts file with the following code:src/app/api/chat/route.ts
.env.local file:.env.local
.env.local accordingly.5
Test your application
To test the application, run That’s it! You’ve successfully integrated first-party tool-calling into your project.Explore the example app on GitHub.
npm run dev and navigate to http://localhost:3000. To interact with the AI agent, you can ask questions like "who am I?" to trigger the tool call and test whether it successfully retrieves information about the logged-in user.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