# Add the chat widget to your own pages

Put Sanka Bot on your intranet, employee portal or internal app. Add your site in Sanka Desk and paste one line of code; employees sign in with a code sent to their work email.

This guide explains how to put Sanka Bot on your company's own pages, such as an intranet, an employee portal or an internal app, with the chat widget. Employees ask from the page they already have open, and each chat becomes a Desk request, as a message to @Sanka in Slack or Teams does. A workspace admin sets it up in **Workspace** → **Integrations** → **Chat widget** in the Sanka Desk console: add your site, paste one line of code into its pages, and the chat appears. Nothing needs to change on your server.

## How it works

A button in the bottom corner of your page opens a chat with Sanka Bot. The chat shows your greeting, up to three suggested questions and the employee's earlier chats from the widget.

- **One chat is one request.** Sanka Bot answers, asks for what it needs, or plans a change, as it does in Slack. A change that needs approval waits for it, as described in [Approve a change and undo it](/docs/desk/approvals/). Your IT team finds these requests in **Tickets**, with **Chat widget** as their channel.
- **Employees sign in with their work email.** The first time, the chat asks for their work email address and sends a code there. Only people Desk finds in your directory get a code, so only they can send requests, as in Slack and Teams. See [Connect your directory](/docs/desk/connect-directory/).
- **Desk trusts a code sign-in as it trusts a request by email.** Sanka Bot answers questions as usual, but anything it would check or change in your systems for the employee, such as an account, access or an app, goes to your IT team: a code read in a mailbox proves less than a chat account. With automatic sign-in (described below), your server vouches for the employee, and Desk trusts the widget as it trusts a Slack or Teams account without enforced single sign-on. Recovering the employee's own sign-in, such as a password reset, still goes to your IT team.
- **Replies come back in the chat.** While the chat is closed, a badge on the button shows how many chats have a reply the employee hasn't read. Employees who are members of your Sanka workspace also get a notification and an email; others see the reply the next time they open the widget.
- **The widget shows only chats started in the widget**, not the person's requests from Slack, Teams or email.

On a screen narrower than 480 pixels, the chat fills the screen. A mobile app can show the widget by opening one of your listed sites in a web view.

## Before you start

- You need to be a workspace admin to open these pages.
- Desk needs to know your employees: connect your directory, or Desk looks among your Sanka members. Each employee needs a work email address Desk can find there, and must be able to read mail sent to it.
- Your site needs to be served over `https://`. Plain `http://` works only on `localhost` and `127.0.0.1`, for development.

## Add the widget to your site

1. In the Desk console, open **Workspace** → **Integrations** → **Chat widget**.
2. Under **1. Add your site**, enter the address your employees open, such as `intranet.example.com`, and choose **Add**. The box already shows `https://`, so type only the host. If you paste a page's full address, only its site is kept.
3. Choose **Save** in the bar at the bottom of the screen. Your first site turns the widget on, and the status next to **Install** says **Live on 1 site**.
4. Under **2. Paste this snippet**, choose **Copy**. Paste the line into your site's HTML just before `</body>`, on every page where the chat should appear.

![Workspace → Integrations → Chat widget, with a site added and the snippet ready to copy](https://static.sanka.com/static/docs/dd8effa1a4f31843f8958a9c27ac687060116cf390fe94dc1178b4fbb6932316.png)

Open one of those pages: the button appears in the bottom corner. The snippet looks like this:

```html
<script async src="https://desk.sanka.com/desk-widget/v1.js" data-app-id="YOUR_APP_ID"></script>
```

Copy it from the settings page rather than from here. It carries your widget's App ID (`dw_` followed by 24 characters) and the right address for the script, which is your partner's address if a partner serves your workspace. The chat runs in a frame served from that same address, so nothing from your page's styles reaches it, and it reads no Sanka sign-in. If your pages send a Content-Security-Policy, allow that address in `script-src` and `frame-src`.

### Sites

The widget shows only on the sites in this list, up to 20.

- Enter the host as the browser shows it, with a port if the site uses one, such as `portal.example.com:8443`. A path is dropped: the whole site is listed.
- The host needs a dot, as in `intranet.example.com`. A one-word host such as `intranet` isn't accepted.
- Each host is its own site. `example.com` and `www.example.com` are two entries, and no wildcards are allowed.
- For a developer's own machine, enter `localhost:3000` or `127.0.0.1:8080`. The box then shows `http://`, which only these two hosts may use.
- A site still in the box when you choose **Save** is added too.

### Show on your sites

Turn **Show on your sites** off and save to take the chat off every site at once. The snippet can stay where it is: the button simply doesn't appear. The switch needs at least one site. Your first site turns it on when you save, unless you turned it off yourself.

Next to **Install**, the page shows **Not set up** while there are no sites, the number of sites while the widget is on, such as **Live on 2 sites**, and **Off** while it is off.

## Try it on your own computer

Before you change your intranet, you can watch the widget work on a test page on your own computer.

1. Download [the test page](/downloads/desk/sanka-desk-chat-widget-test.html).
2. Open the file in a text editor. Near the end, in the line that starts with `<script async`, replace the App ID after `data-app-id=` with the one in your snippet, or paste your snippet over that line.
3. In **Workspace** → **Integrations** → **Chat widget**, add `localhost:8000` under **1. Add your site**, and choose **Save**.
4. In the folder that holds the file, run `python3 -m http.server 8000`, then open `http://localhost:8000/sanka-desk-chat-widget-test.html`.
5. Choose the chat button in the bottom corner, and sign in with your work email and the code Desk sends you.

![The test page on localhost:8000, with the chat open on its sign-in screen](https://static.sanka.com/static/docs/32fa5d08484980e3a6db37f6ea17b55fca5893381ff8016e9e377a753600e7e0.png)

Opening the file directly (`file://`) doesn't work: the widget runs only on a site you added. When you're done, you can remove `localhost:8000` again.

## What employees see

1. On a listed site, while the widget is on, the button appears in the bottom corner of the page. On any other site it doesn't appear.
2. The first time, the chat asks for their **Work e-mail**, and they choose **Send code**.
3. Desk emails them an 8-digit code. The email names your site, and the code works for 10 minutes.
4. They enter the code under **Code**. A complete code is checked by itself; **Continue** does the same.
5. They stay signed in for 30 days in that browser on that site. **Signed in as** and their address, with **Sign out**, are under the message box on the chat's first screen.

The chat answers the same way for every address it is given: **We sent a sign-in code to … if it is in your company's directory.** Only people in your directory get the email, so the chat never tells anyone whether an address belongs to your company. If no email arrives, check the address and the spam folder, and that the person is in your directory.

Each site, browser and device signs in separately, and so does a private window. **Sign out** ends the sign-in on that browser and site.

The chat is in Japanese when the employee's browser is set to Japanese, and in English otherwise, and the code email follows the chat. To fix the language, add `data-language="ja"` or `data-language="en"` to the snippet. Your greeting and suggestions are shown as you write them.

## Appearance and preview

The preview beside the settings shows the chat as employees will see it on your site, with your changes before you save them. Switch it between **Chat**, **Sign-in** and **Closed** (the button alone).

![The preview, drawn from the unsaved settings](https://static.sanka.com/static/docs/f07274b84d925016f0f331fe1ed2e81f133fee76b4c40f2243097cd5c618b4e5.png)

| Setting | What it does |
| --- | --- |
| **Accent colour** | The colour of the button and of the employee's own messages, as `#RRGGBB`, or chosen with the colour square. **Default** goes back to the default colour. |
| **Position** | **Bottom right** or **Bottom left** of the page. |
| **Greeting** | Up to 200 characters, shown at the top of the chat. Left empty, the widget greets the employee by first name and asks what they need from IT. |
| **Suggestions** | Up to three questions, 80 characters each, that employees can send with one click, such as "I can't sign in to the VPN". |
| **Show “Powered by Sanka”** | A small line under the chat. It is on until you turn it off. |

If a partner serves your workspace, **Show “Powered by Sanka”** can't be changed here: the page says **Your partner decides whether it shows.**

## Save your changes

![The bar at the bottom of the screen while changes are unsaved](https://static.sanka.com/static/docs/b505e311ae10214a6ba9c3ab14eb34f373b7d4ac5a9a93716cfd7d217f5ec889.png)

While anything is unsaved, a bar at the bottom of the screen says **Unsaved changes**, with **Discard** and **Save**. Sites, appearance and **Show on your sites** take effect when you save, and leaving the page with unsaved changes asks first. Automatic sign-in is separate: its key takes effect as soon as you make it.

## Try it live

**Try it live**, under the preview, opens the real widget on the settings page, signed in as you. It works before you add a site and while the widget is off.

What you send is a real request: your IT team sees it in **Tickets**, and the work Sanka Bot finishes on it is [charged in Sanka Credits](/docs/desk/pricing/). Because it signs you in as yourself, you need to be someone Desk can find in your directory. If **Try it live** says **Something went wrong. Try again.**, check that first.

## Automatic sign-in (optional)

If your site already knows who is signed in, for example an intranet behind your single sign-on, employees can skip the email code. Your server signs a short token for the signed-in employee with a secret key, and the page passes it to the widget. This part needs a developer.

1. Under **Automatic sign-in**, choose **Set up**.
2. Copy the key and store it on your server. Sanka shows it only once: afterwards the page shows only its last four characters and when it was made. Choose **I've stored it**.
3. Give your developer the key, and point them to the code under **Automatic sign-in** and the rest of this section. Pass the key the way your company passes passwords, not in a chat or an email.

The key works as soon as you make it. Anyone who has it can chat as any employee, so keep it on your server only.

### 1. Sign a token on your server

The token is a JSON Web Token (JWT) signed with HS256 and the widget's key.

| Claim | Required | Value |
| --- | --- | --- |
| `email` | Yes | The employee's work email address, as your directory has it. Desk looks the person up by this address. |
| `name` | No | The employee's name. The widget greets the employee by it only when your directory has no name for them. |
| `iat` | Yes | When you signed it, in seconds since 1970. |
| `exp` | Yes | When it expires. Sign for 10 minutes. It may be at most an hour after `iat`. |
| `aud` | Recommended | Your App ID. A token whose `aud` is anything else is refused. |

Desk allows 30 seconds of difference between your server's clock and its own. Sign a new token on each page load, and never sign in the browser: the key must stay on your server.

Node.js, with the `jsonwebtoken` package:

```js
import jwt from "jsonwebtoken";

// For the signed-in employee, on each page load.
const token = jwt.sign(
  { email: user.email, name: user.name },
  process.env.SANKA_DESK_WIDGET_SECRET,
  { algorithm: "HS256", audience: "YOUR_APP_ID", expiresIn: "10m" },
);
```

Python, with PyJWT:

```python
import os, time
import jwt  # PyJWT

now = int(time.time())
token = jwt.encode(
    {"email": user.email, "name": user.name, "iat": now, "exp": now + 600,
     "aud": "YOUR_APP_ID"},
    os.environ["SANKA_DESK_WIDGET_SECRET"],
    algorithm="HS256",
)
```

### 2. Pass the token to the widget

On pages that sign employees in automatically, use this instead of the snippet, with the token your server signed for this page load:

```html
<script>
  window.SankaDesk = window.SankaDesk || function () { (SankaDesk.q = SankaDesk.q || []).push(arguments); };
  SankaDesk("boot", { appId: "YOUR_APP_ID", token: "<TOKEN_FROM_YOUR_SERVER>" });
</script>
<script async src="https://desk.sanka.com/desk-widget/v1.js"></script>
```

Copy it from **Automatic sign-in** to get your App ID and the right address for the script. The `window.SankaDesk = …` line lets your page call `SankaDesk` before the script has loaded; the calls wait until it has.

### Keep employees signed in with getToken

The widget exchanges your token for a session of its own. In one browser tab, the session lasts up to 12 hours while the employee moves between your pages, as long as the same person is signed in. When it ends, for example after 12 hours or when an admin replaces the key, the widget needs a new token.

Pass `getToken`, an async function that returns a newly signed token, and the widget asks for one by itself. Without it, the widget has only the token it booted with, and the employee has to reload the page.

```html
<script>
  window.SankaDesk = window.SankaDesk || function () { (SankaDesk.q = SankaDesk.q || []).push(arguments); };
  SankaDesk("boot", {
    appId: "YOUR_APP_ID",
    token: "<TOKEN_FROM_YOUR_SERVER>",
    // An endpoint on your own server that signs a new token for the signed-in employee.
    getToken: async function () {
      const response = await fetch("/sanka-desk-token", { credentials: "same-origin" });
      const body = await response.json();
      return body.token;
    },
  });
</script>
```

The widget uses `token` first and calls `getToken` after that. You can also pass only `getToken`. If the widget starts with no token to use, because `getToken` fails or returns nothing, the employee signs in with an email code instead. If `getToken` fails later, after a session ends, the chat keeps its message until the employee reloads the page.

### Replace or turn off the key

- **Replace key** asks **Replace the secret key?**. After you choose **Replace**, the old key stops working at once, and employees it signed in are signed out until your server signs with the new key. Put the new key on your server straight away.
- **Turn off** asks **Turn off automatic sign-in?**. After you confirm, the key stops working at once and employees sign in with an email code instead, even on pages that still pass a token.

Neither affects employees who signed in with a code. Replace the key when it may have leaked, when someone who knew it leaves, or when nobody can find it: anyone holding it can chat as any employee, and Sanka can't show it again.

## JavaScript API (for developers)

Every command goes through `SankaDesk(command, ...)`. On a page with only the snippet, the commands work once its script has loaded. To pass `token`, `getToken` or `hideLauncher`, load the widget with `boot` instead of the snippet, as in the automatic sign-in code above; without `token` or `getToken`, employees sign in with an email code.

| Command | What it does |
| --- | --- |
| `SankaDesk("boot", options)` | Starts the widget. `appId` is required. `token` or `getToken` signs the employee in automatically. `language: "ja"` or `language: "en"` sets the chat's language; without it, the browser's language decides. `hideLauncher: true` hides the button, so you open the chat from your own. Calling `boot` again replaces the widget. When a different person signs in to your page in the same browser, call `shutdown` before `boot`: otherwise the new widget can reopen the previous person's code sign-in. |
| `SankaDesk("open")`, `SankaDesk("close")`, `SankaDesk("toggle")` | Open, close or toggle the chat. They do nothing until the widget is on for the site (the `ready` event). |
| `SankaDesk("newChat", { text })` | Opens the chat on a new conversation with `text` already in the message box. The employee still sends it. |
| `SankaDesk("shutdown")` | Ends the employee's widget session, including a code sign-in on this browser, and removes the widget. Call it when the employee signs out of your page. |
| `SankaDesk("on", event, fn)`, `SankaDesk("off", event, fn)` | Adds or removes a listener. Events: `ready` (the widget is on for this site and can open), `open`, `close`, `unread` (the number of chats with an unread reply), `error` (`{ code, reason }`, when the widget can't run on this site or can't sign the employee in with your token). |

For example, to open the chat from your own help link and show the unread count yourself:

```html
<script>
  window.SankaDesk = window.SankaDesk || function () { (SankaDesk.q = SankaDesk.q || []).push(arguments); };
  SankaDesk("boot", { appId: "YOUR_APP_ID", hideLauncher: true });
  document.querySelector("#it-help").addEventListener("click", function () {
    SankaDesk("open");
  });
  SankaDesk("on", "unread", function (count) {
    document.querySelector("#it-help-count").textContent = count ? String(count) : "";
  });
</script>
<script async src="https://desk.sanka.com/desk-widget/v1.js"></script>
```

## Changes that affect people using the widget

| Change | What happens |
| --- | --- |
| Turning **Show on your sites** off and saving | Every widget session ends at once. Requests already open carry on in Desk. The button no longer appears on your pages once they reload. When you turn it on again, employees sign in again. |
| Removing a site and saving | Sessions on that site end for good, and the button no longer appears there. Adding the site again doesn't bring them back: employees sign in again. Other sites aren't affected. |
| **Replace key** under **Automatic sign-in** | Sessions from automatic sign-in end at once. Until your server signs with the new key, the chat says **The chat couldn't confirm who you are.** |
| **Turn off** under **Automatic sign-in** | Sessions from automatic sign-in end at once, and employees sign in with an email code instead. |
| Someone leaves the directory | Desk uses its last answer from your directory for up to a day. Once that answer is a day old, the widget's next request asks again: the person's session ends, and they can't send anything or get a new code. Where Desk uses your Sanka members instead of a directory, it checks every time. |

A page that is already open notices within about half a minute, as the widget checks for replies. Someone who signed in with a code then sees the sign-in again. Someone signed in automatically sees **Your session ended. Reload the page to sign in again.**, and pages that pass `getToken` sign them in again by themselves.

## Troubleshooting

### The button doesn't appear

The widget stays hidden until Desk confirms it is on for the page's site. Open the browser's developer console on the page: the widget says why it is hidden.

| The console says | What to do |
| --- | --- |
| `[Sanka Desk] this site (…) is not on the chat widget's list. Add it in Sanka Desk → Setups → Chat widget.` | Add the site in **Workspace** → **Integrations** → **Chat widget** and save. The scheme, host and port must match exactly: `www.example.com` and `example.com` are different sites. |
| `[Sanka Desk] the chat widget is off, or its appId is wrong. Turn it on in Sanka Desk → Setups → Chat widget.` | Turn on **Show on your sites** and save, and check that the snippet's `data-app-id` matches the one on the settings page. Copy the snippet again if in doubt. This also shows while Desk is off for your workspace. |
| `[Sanka Desk] boot needs the widget's appId (dw_…) from Sanka Desk → Setups → Chat widget.` | The App ID in `data-app-id` or in `boot` is missing or mistyped. Copy the snippet again. |
| Nothing from Sanka Desk | The script didn't load. Check that the snippet is on the page, and that your Content-Security-Policy allows the script's address in `script-src` and `frame-src`. |

### Sign-in codes

| The chat says | Why |
| --- | --- |
| That code isn't right. 4 tries left. | Each code allows five tries, and the message counts down. |
| Too many tries. Send a new code. | The fifth wrong try ends the code. **Send a new code** sends another. |
| That code has expired. Send a new one. | The code is older than 10 minutes, or was already used. |
| Too many codes for this address. Try again later. | One address gets at most 5 codes an hour and 20 a day, and no more codes that day once it has had 10 wrong tries. |
| Too many wrong codes for this address today. Try again tomorrow, or ask your IT team. | The address has had 10 wrong tries in a day, counted over all its codes. |
| Too many messages at once. Wait a minute and try again. | More than 60 codes, or 120 code checks, in a minute from one network address. |

The hourly and daily limits count per email address, so one person's mistakes don't lock out a colleague.

### Messages in the chat

| The chat says | Code | Why |
| --- | --- | --- |
| This chat isn't available right now. | `DESK_WIDGET_UNAVAILABLE` | The widget was turned off, or Desk is off for the workspace. |
| This site isn't allowed to run this chat. Your IT team can add it in Sanka Desk. | `DESK_WIDGET_ORIGIN_NOT_ALLOWED` | The page's site was removed in **Workspace** → **Integrations** → **Chat widget**. |
| Your account can't send requests here. Ask your IT team to add you to the company directory. | `DESK_REQUESTER_NOT_IN_DIRECTORY` | Desk can't find the person in your directory, or the account is suspended. |
| Your session ended. Reload the page to sign in again. | `DESK_WIDGET_SESSION_INVALID` | A session from automatic sign-in ended: 12 hours passed, the key was replaced or turned off, the widget was turned off, the site was removed, or the person left the directory. With `getToken`, the widget asks for a new token by itself. |
| The chat couldn't confirm who you are. Reload the page and try again. | `DESK_WIDGET_TOKEN_INVALID` | Desk refused the page's token. The chat also shows **For your developers: token** and one of the reasons below. |
| Too many messages at once. Wait a minute and try again. | `DESK_WIDGET_RATE_LIMITED` | More than 20 new chats and messages a minute in one widget session, or more than 300 automatic sign-ins a minute from one network address. |
| This request is finished. Start a new chat to ask again. | `DESK_WIDGET_CHAT_CLOSED` | The request is finished and takes no more messages. **New chat** starts another. |

When Desk refuses a token, the reason tells your developers what to fix. The browser console shows it too, as `[Sanka Desk] chat widget: DESK_WIDGET_TOKEN_INVALID (signature)`, and your page receives it through the `error` event.

| Reason | What is wrong |
| --- | --- |
| `malformed` | It isn't a JWT, or it was cut short. |
| `algorithm` | It isn't signed with HS256. |
| `signature` | It was signed with a different key: an old one after it was replaced, or one copied wrong. |
| `expired` | Its `exp` has passed. |
| `lifetime` | Its `exp` is more than an hour after `iat`, or more than an hour from now. |
| `claims` | `email`, `iat` or `exp` is missing, `email` isn't an email address, or `iat` or `nbf` is in the future. |
| `audience` | Its `aud` isn't this widget's App ID. |
| `no_key` | Automatic sign-in is off, so the token isn't used. The chat shows the code sign-in instead, and the console says `[Sanka Desk] automatic sign-in is off for this widget, so the page's token is not used: employees sign in with a code.` |

A token longer than 4,096 characters is refused before Desk reads it: the chat says only **Something went wrong. Try again in a moment.**, and the console says `[Sanka Desk] chat widget: VALIDATION_ERROR`.

The widget also writes these to the console on pages that pass tokens:

| The console says | What happened |
| --- | --- |
| `[Sanka Desk] getToken returned no token: employees sign in with a code instead.` | When the widget started, `getToken` returned nothing. |
| `[Sanka Desk] could not get a token for the chat widget: employees sign in with a code instead.` | When the widget started, `getToken` failed. |
| `[Sanka Desk] getToken failed.` | `getToken` failed when the widget asked for a new token. The employee has to reload the page. |
| `[Sanka Desk] the chat widget's token was expired; pass getToken to boot so it can ask again.` | The session ended or the token expired, and the page has no `getToken`. It says `refused` instead when Desk refused the token for another reason. |

## What the widget doesn't do yet

- It is for your employees only, the people in your directory. It isn't for your customers or other visitors to a public site.
- Employees can't attach files or images in the chat.
- Knowledge articles, Catalog forms and the satisfaction question aren't in the widget.
- Employees who aren't members of your Sanka workspace get no notification of a reply. They see it when they open the widget again.
- A workspace has one widget.
- On a site with automatic sign-in, an employee whose page passes no token can still sign in with an email code: there is no switch yet to turn the code off.
- There are no iOS or Android SDKs and no npm package. In a mobile app, open one of your listed sites in a web view.
