> ## Documentation Index
> Fetch the complete documentation index at: https://nuggets.life/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Agent Authentication

> Secure AI agent authentication with cryptographic verification. Create authenticated agents, download private keys, and verify tokens to prevent bad actors.

Nuggets gives your AI Agent a secure way to prove it is not a bad actor.

<div style={{ backgroundColor:"white",padding:"1rem",borderRadius:"1rem" }}>
  <iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/8J3nTzpWRac?rel=0" title="Embedded Video" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />
</div>

<Card title="Demo" icon="github" href="https://github.com/NuggetsLtd/ai">
  See an example of Nuggets authentication integrated with Google's A2A protocol
  demo.
</Card>

## Getting Started

<Steps>
  <Step title="Create an AI Agent">
    Go to the [Nuggets Account Portal](https://accounts.nuggets.life) and navigate to the AI Agents page to create your first agent.

    <Frame caption="AI Agent - Menu Item">
      <img src="https://mintcdn.com/nuggets-b89005a2/nUdoZWSc5gGcRIEv/assets/accounts-portal/ai-agent/checks.png?fit=max&auto=format&n=nUdoZWSc5gGcRIEv&q=85&s=e5009a9decb3e6512aae310911f92ebe" alt="" width="1561" height="507" data-path="assets/accounts-portal/ai-agent/checks.png" />
    </Frame>
  </Step>

  <Step title="Download Private Key and Save Client ID">
    Once created, you will be prompted to download the private key for this client. **It is important you save this file.**

    <Warning>
      This key is only available once and is needed to successfully authenticate your AI Agent.
    </Warning>

    <Frame caption="AI Agent - Private Key Download">
      <img src="https://mintcdn.com/nuggets-b89005a2/nUdoZWSc5gGcRIEv/assets/accounts-portal/ai-agent/private-key.png?fit=max&auto=format&n=nUdoZWSc5gGcRIEv&q=85&s=24702dd706b3b1567dccc203798eb1d5" alt="" width="612" height="484" data-path="assets/accounts-portal/ai-agent/private-key.png" />
    </Frame>
  </Step>

  <Step title="(Optional) Add Verified Information">
    Within Nuggets, you can build greater trust by adding verified information—such as a validated social media account for either the AI Agent or its creator.

    <Frame caption="AI Agent - Menu Item">
      <img src="https://mintcdn.com/nuggets-b89005a2/nUdoZWSc5gGcRIEv/assets/accounts-portal/ai-agent/created.png?fit=max&auto=format&n=nUdoZWSc5gGcRIEv&q=85&s=004057a338422d4dae0b63d0fb169da8" alt="" width="1561" height="817" data-path="assets/accounts-portal/ai-agent/created.png" />
    </Frame>

    <Frame caption="Advanced Check - Invite Verifications">
      <img src="https://mintcdn.com/nuggets-b89005a2/nUdoZWSc5gGcRIEv/assets/accounts-portal/advanced-check/verifications-qr-code.png?fit=max&auto=format&n=nUdoZWSc5gGcRIEv&q=85&s=b21ed901d3f568a8794001910813904f" alt="" width="466" height="984" data-path="assets/accounts-portal/advanced-check/verifications-qr-code.png" />
    </Frame>
  </Step>
</Steps>

## Authenticating The Agent

Now you have created the Agent inside the Accounts Portal, we can use the created details to authenticate the agent.

<Tabs>
  <Tab title="Using @nuggetslife/auth">
    ### 1. Install the authentication package.

    To get started, firstly install the authentication package on the `agent` and `server`.

    ```bash theme={null}
    npm install @nuggetslife/auth
    ```

    Next, you'll need to add the private key and the DID provided from the accounts portal to your `agent` environment:

    ```bash theme={null}
    export NUGGETS_DID=
    export NUGGETS_PRIVATE_JWK=
    ```

    ### 2. Expose an authentication endpoint on the Agent.

    Expose an endpoint on your agent which allows for the JWT token to be generated (via `createAuthenticationToken`). Under the hood, this uses your downloaded private key and assigns the DID to the payload.

    ```typescript Create the authentication token on the agent (express example). theme={null}
    // # Agent
    import type { Request, Response } from "express";
    import { createAuthenticationToken } from "@nuggetslife/auth/ai";

    app.get("/authenticate", async (req: Request, response: Response) => {
      const token = await createAuthenticationToken();
      res.send(token);
    });
    ```

    ### 3. Verify the token

    Once the requesting `server` receives the token, you are then able to verify it using the `verifyToken` function. If successful, this will return the DID Document for the client. If unsuccessful, the agent is potentially a bad actor.

    <Tip>
      You can get the details from your clients DID Document by extracting the
      client ID from your DID and navigating to\` \`
      `https://auth.nuggets.life/{CLIENT_ID}/.well-known/did.json`
    </Tip>

    ```javascript Authenticate the agents token. theme={null}
    import { verifyToken } from "@nuggetslife/auth/ai";

    function getTokenFromAgent() {
      return fetch("AGENT_URL/authenticate").then((res) => res.json());
    }

    export async function authenticate() {
      try {
        const token = await getTokenFromAgent();
        const { did } = await verifyToken(token);
      } catch (error) {
        console.error("Error verifying the token");
      }
    }
    ```

    #### Verify token response

    <ResponseField name="did" type="object">
      The DID Document resolved from the DID in the token.
    </ResponseField>

    <ResponseField name="services" type="object">
      An object of services

      <ParamField path="services.VerifiedInformationAPI" type="string">
        Returns the URL to get the verified information as JSON
      </ParamField>

      <ParamField path="services.VerifiedInformationHTML" type="string">
        See a hosted page with the details of you AI Agent
      </ParamField>
    </ResponseField>

    ### 4. Additional Information

    Once a successful connection is made, you're then able to retrieve more information about the `agent` via the `verifiedDetails` call. This calls the `service`s that are returned as part of the client's DID Document.

    <Tip>
      You can get the verified information about your client by **extracting** the
      client ID from your DID and navigating to\` \`
      `https://auth.nuggets.life/verified-info/{CLIENT_ID}/json`
    </Tip>

    ```typescript Get Verified Information theme={null}
    import { getVerifiedDetails } from "@nuggetslife/auth/ai";

    export async function authenticate() {
      try {
        const token = await getTokenFromAgent();
        const { did } = await verifyToken(token);
        const verifiedDetails = await verifiedDetails(did);
      } catch (error) {
        console.error("Error verifying the token");
      }
    }
    ```
  </Tab>

  <Tab title="Manual">
    ### 1. Add your environment variables

    ```bash theme={null}
    export NUGGETS_DID=
    export NUGGETS_PRIVATE_JWK=
    ```

    ### 2. Generate Token

    On your `agent`, start by creating an authentication endpoint that your `server` can call. This endpoint should return a JWT containing the DID, signed with your private key.

    ### 3. Verifying the Agent’s Identity with `did:web`

    Once the server receives the JWT from the agent’s authentication endpoint, it must decode and verify it using the DID provided in the token.

    <Steps>
      <Step title="Extract the DID from the JWT">
        The JWT payload should include a sub or custom claim containing the agent’s
        DID:

        `did:web:auth.nuggets.life:CLIENT_ID`
      </Step>

      <Step title="Resolve the DID Document">
        To verify the JWT, the server must resolve the DID to retrieve the associated public key.

        For `did:web`, the DID translates to a standard HTTPS URL. Replace : with / after did:web: and append /did.json. For example:

        `did:web:auth.nuggets.life:CLIENT_ID → https://auth.nuggets.life/CLIENT_ID/did.json`
      </Step>

      <Step title="Verify the JWT">
        Use the public key found in the `verificationMethod` section of the DID
        Document to verify the JWT's signature. If successful your AI Agent will be
        authenticated
      </Step>
    </Steps>

    ### 4. Accessing Additional Agent Information

    Once you’ve successfully authenticated the agent and verified the JWT, you'll have access to the agent’s valid DID Document.

    Using the `service` field in this document, you can retrieve additional information provided by the agent. This may include:

    * Ownership details
    * Verified social media accounts
    * Other relevant metadata

    This allows your application to build a richer, more trustworthy profile of the agent.
  </Tab>
</Tabs>
