## AI Identity for B2B

AI Identity for B2B leverages [Auth0 FGA](/content/fine-grained-authorization/index.html) to provide fine-grained authorization control for AI agents. As a result, when AI agents use Retrieval Augmented Generation (RAG) to provide sophisticated, relevant responses to user queries, they only have access to authorized data. By the end of this quickstart, you should have an AI application that can:

1. Retrieve authorized data as context for a RAG pipeline.
2. Use Auth0 FGA to determine if the user has authorization for the data.

## Pick your tech stack

-  LangGraph.js + Next.js
-  Vercel AI + Next.js
-  LangGraph + FastAPI
-  LangGraph.js + Node.js
-  LangGraph + Python

### Prerequisites

Before getting started, make sure you:

1. **Create an Auth0 Account**
   To continue with this quickstart, you need to have an [Auth0 account.](/content/signup?onboard_app=auth_for_aa&ocid=701KZ000000cXXxYAM-aPA4z0000008OZeGAM/index.html)

2. **Create an Auth0 Application**
   Go to your [Auth0 Dashboard](https://manage.auth0.com/dashboard) to create a new Auth0 Application.
   - Navigate to [**Applications > Applications**](https://manage.auth0.com/#/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. **Create an Auth0 API**
   - In your Auth0 Dashboard, go to **Applications > APIs**.
   - Create a new API with an identifier (audience).
   - Once API is created, go to the APIs **Settings > Access Settings** and enable **Allow Offline Access**.
   - Note down the API identifier for your environment variables.

4. **Create an Auth0 FGA account**  
   You need an [Auth0 FGA account](https://dashboard.fga.dev/) to complete this quickstart.

5. **OpenAI Platform**
   Set up an [OpenAI account and API key](https://platform.openai.com/docs/libraries#create-and-export-an-api-key).

### Download sample app

Start by downloading and extracting the sample app. Then open in your preferred IDE.

### Install dependencies

In the root directory of your project, install the following dependencies:

```bash
npm install @auth0/ai-langchain @langchain/langgraph langchain @langchain/core @langchain/openai zod langgraph-nextjs-api-passthrough
```

### 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.

### Set up an FGA Store

In the [Auth0 FGA dashboard](https://dashboard.fga.dev/):

1. Navigate to **Settings**. In the **Authorized Clients** section, click **+ Create Client**.
2. Give your client a name and mark all the client permissions that are required for your use case. For the quickstart, you’ll only need **Read and query**.
3. Click **Create**.

Once your client is created, you’ll see a modal containing Store ID, Client ID, and Client Secret. Add or update an `.env.local` file with the following content to the root directory of the project:

```
OPENAI_API_KEY=<your-openai-api-key>
FGA_STORE_ID=<your-fga-store-id>
FGA_CLIENT_ID=<your-fga-store-client-id>
FGA_CLIENT_SECRET=<your-fga-store-client-secret>
FGA_API_URL=https://api.xxx.fga.dev
FGA_API_AUDIENCE=https://api.xxx.fga.dev/
```

### Secure the RAG Tool

After configuring your FGA Store, secure the RAG tool using Auth0 FGA and Auth0 AI SDK. The starter application is already configured to handle documents and embeddings.  
**Document Upload and Storage**
- You can upload documents through the UI (`src/app/documents/page.tsx`).
- Uploaded documents are processed by the API route (`src/app/api/documents/upload/route.ts`).
- APIs for uploading and retrieving documents are defined in (`src/lib/actions/documents.ts`).
- Database is defined in `src/lib/db`.
- FGA helpers are defined in `src/lib/fga`.
- Documents are stored as embeddings in a vector database for efficient retrieval (`src/lib/rag/embedding.ts`).

**Access Control with Auth0 FGA**
When a document is uploaded, the app automatically creates [FGA tuples](https://docs.fga.dev/fga-concepts#what-is-a-relationship-tuple) to define which users can access which documents. A tuple signifies a user’s relation to a given object. For example, the below tuple implies that all users can view the `<document name>` object.

### Create a RAG tool

Define a RAG tool that uses the `FGAFilter` to filter authorized data from the vector database:

```typescript
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { FGARetriever } from "@auth0/ai-langchain/RAG";

export const getContextDocumentsTool = tool(async ({ question }, config) => {
    const user = config?.configurable?._credentials?.user;
    if (!user) {
      return "There is no user logged in.";
    }
    const vectorStore = await getVectorStore();
    if (!vectorStore) {
      return "There is no vector store.";
    }
    const retriever = FGARetriever.create({
      retriever: vectorStore.asRetriever(),
      buildQuery: (doc) => ({
        user: `user:${user?.email}`,
        object: `doc:${doc.metadata.documentId}`,
        relation: "can_view",
      }),
    });
    // filter docs based on FGA authorization
    const documents = await retriever.invoke(question);
    return documents.map((doc) => doc.pageContent).join("\n\n");
});
```

### Test your application

Start the database and create required tables:

```bash
# start the postgres database
docker compose up -d
# create the database schema
npm run db:migrate
```

Start the application with `npm run all:dev`. Then, 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.
Upload a document from the documents tab and ask your AI agent a question about the document. You should get a response with the relevant information.

Go to an incognito window, log in as a different user, and ask it the same question. You should not get a response. Share the document from the documents page to the second user and try again. You should see the information now.

That’s it! You successfully integrated RAG protected by Auth0 FGA into your project. Explore [the example app on GitHub](https://github.com/auth0-samples/auth0-ai-samples/tree/main/authorization-for-rag/langchain-next-js).

### Next steps
- [Authorization for RAG docs](/content/ai/docs/intro/authorization-for-rag/index.html)
- Learn how to use [Auth0 FGA](/content/fine-grained-authorization/index.html) to create a Relationship-Based Access Control (ReBAC) authorization model.
- Learn more about [OpenFGA](https://openfga.dev/docs/fga).
