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
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:call-apis-on-users-behalf/your-api/langchain-next-js directory.- VS Code
- Cursor
- Claude Code
- Claude
or Follow manual steps
1
Install dependencies
In the root directory of your project, install the following dependencies:
@langchain/langgraph: The core LangGraph module.@langchain/openai: OpenAI provider for LangChain.langchain: The core LangChain module.zod: TypeScript-first schema validation library.langgraph-nextjs-api-passthrough: API passthrough for LangGraph.
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
Pass credentials to the agent
You have to pass the access token from the user’s session to the agent. First, create a helper function to get the access token from the session. Add the following function to Now, update the
src/lib/auth0.ts:src/lib/auth0.ts
/src/app/api/chat/[..._path]/route.ts file to pass the access token to the agent:src/app/api/chat/[..._path]/route.ts
4
Define a tool to call your API
In this step, you’ll create a LangChain 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
5
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. Update 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
/src/lib/agent.ts file to add the tool to the agent.src/lib/agent.ts
.env.local file:.env.local
.env.local accordingly.6
Test your application
To test the application, run To interact with the AI agent, you can ask questions like That’s it! You’ve successfully integrated first-party tool-calling into your project.
npm run all:dev and navigate to http://localhost:3000.This will open the LangGraph Studio in a new tab. You can close it as we won’t
require it for testing the application.
"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