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

# Chat widget

> Add an AI agent to your site that chats with your visitors.

Create the agent in the app under **Agents → Inbound agents**. Its **Widget** tab gives you this snippet:

```html theme={null}
<script src="https://app.revenuebee.ai/widget.js" async></script>
<script>
  window.gdwidget =
    window.gdwidget ||
    function () {
      (window.gdwidget.q = window.gdwidget.q || []).push(arguments);
    };
  gdwidget({ key: "ia_…", baseUrl: "https://api.revenuebee.ai" });
</script>
```

* `key` is your agent's key, and `baseUrl` is your CRM's API address.
* Your site has to be in the agent's allowed sites, or the widget won't show.

## Signed-in visitors

Each new visitor becomes a new contact. If they're signed in to your site, tell the agent who they are, so it can help with their account.

On your server:

> [tab] Upsert
>
> ```ts theme={null}
> // the signed-in user, as a contact: created the first time, found every time after
> const [contact, contactError] = await crm.contacts.upsert({
>   dbId: user.id,
>   name: user.name,
>   emails: [user.email],
> });
> if (contactError) throw new Error(contactError.message);
>
> // a claim for that contact, for the page to pass to the widget
> const [claim, claimError] = await crm.chat.mintClaim({ partyId: contact.id });
> if (claimError) throw new Error(claimError.message);
> ```

> [tab] Look up
>
> ```ts theme={null}
> // the customer you already sent, found by your own id
> const [found, lookupError] = await crm.contacts.resolve({ dbId: user.id });
> if (lookupError) throw new Error(lookupError.message);
>
> // a claim for that contact, for the page to pass to the widget
> const [claim, claimError] = await crm.chat.mintClaim({ partyId: found.partyId });
> if (claimError) throw new Error(claimError.message);
> ```

> [tab] Saved id
>
> ```ts theme={null}
> // the contact.id you saved in your own database the first time
> const [claim, claimError] = await crm.chat.mintClaim({ partyId: user.crmPartyId });
> if (claimError) throw new Error(claimError.message);
> ```

**Upsert** creates the customer if they're new. **Look up** finds one you've already sent. **Saved id** is fastest: store `contact.id` in your own database the first time, and skip the extra call.

On the page:

```html theme={null}
<script>
  gdwidget({
    key: "ia_…",
    baseUrl: "https://api.revenuebee.ai",
    secureClaim: "<claim.claim from your server>",
  });
</script>
```

Now the visitor sees their past chats on any device, and the agent can use the tools you marked **secure**.

## Good to know

* **Without a claim**, an email a visitor types is saved, but never matched to a customer you already have.
* **A claim lasts six hours.** Change it with `ttlSeconds`, up to a day. Make a new one on each page load.
* **A claim only tells the agent who the visitor is.** It gives no access to your API.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.