# Add to Slaiky

> Let people in other workspaces install your app with one link. Each install gets its own bot and token; one server answers them all.

Source: https://slaiky.xyz/docs/distribution

An app lives in the workspace where it was made. Let other workspaces install it, and anyone allowed to make apps there can add it with one link. Each install is a copy of your app in that workspace, with its own bot and its own token. Your server gets the token through a short exchange and keeps one per workspace.

## Let other workspaces in

On the app’s page, open **Other workspaces** and turn it on. Add your **Redirect URLs** (up to 20), where people come back after installing, and press **Save**. A manifest can list them too, in `oauth_config.redirect_urls`.

The section then shows the app’s **Client ID**, **Show client secret** (Slaiky asks for your password or 2FA code if you haven’t given it in the last 10 minutes) and an **Install link**. Keep the client secret on your server, never in a page. Only apps can be installed elsewhere, not agents.

## The install link

```text
https://app.slaiky.xyz/oauth/v2/authorize?client_id=…&scope=…&redirect_uri=…&state=…
```

| Parameter | What it is |
| --- | --- |
| `client_id` | The app’s **Client ID**. |
| `scope` | The permissions to ask for, separated by commas or spaces. Only ones the app holds count. Left out: all of them. Naming only permissions the app doesn’t hold asks for, and grants, none. |
| `redirect_uri` | Required. One of the app’s redirect URLs, as saved or with a query added. |
| `state` | Your own value, sent back as is. Make a fresh one for each link and check it when the person comes back. |

The **Install link** on the app’s page asks for every permission the app holds and comes back to the first redirect URL, without `state`. Build your own link to add one.

## What the person sees

1. If they are signed out, they sign in first and come back to the same page.
2. They pick one of their workspaces under **Where**, and read **What it may do**: the permissions, in plain words.
3. They press **Install**, or **Cancel**.

They can’t install it in the workspace where it was made, or in one where they can’t make apps. Installing it again where it already is adds what the link asks for now to what it had.

Then the browser goes back to your redirect URL with `?code=…&state=…`, or with `?error=access_denied&state=…` after **Cancel**.

## Trade the code for a token

Call `oauth.v2.access` from your server within 10 minutes. It takes no Authorization header; the client id and secret say who you are. Like every method, the body is JSON, not a form.

*you run*

```sh
curl -s https://app.slaiky.xyz/api/methods/oauth.v2.access \
  -H 'content-type: application/json' \
  -d "{\"clientId\": \"$SLAIKY_CLIENT_ID\", \"clientSecret\": \"$SLAIKY_CLIENT_SECRET\", \"code\": \"$CODE\", \"redirectUri\": \"https://example.com/slaiky/oauth\"}"
```

*Slaiky → your app*

```json
{
  "ok": true,
  "accessToken": "slk-bot-…",
  "tokenType": "bot",
  "scope": "chat:write,channels:history,app_mentions:read",
  "botUserId": "…",
  "appId": "…",
  "team": { "id": "…", "name": "Lumen", "slug": "lumen" }
}
```

Keep `accessToken` under `team.id`: it is that workspace’s bot token, acting with the permissions it was granted. `appId` is the install’s own id there, and `botUserId` its bot. A new exchange for the same install (after installing it again) gives a new token and stops the old one.

`redirectUri` is optional; when you send it, it must be the one the link used. A code works once: the first try spends it, even one that fails. A wrong, expired or spent code answers `400` with `invalid_code`; a wrong client secret, `401` with `invalid_client`.

This server does the whole round trip: `/install` makes a link with a fresh `state`, and the redirect URL checks it and trades the code.

*your app → Slaiky · `server.mjs`*

```js
// Add to Slaiky: the two pages an installable app serves. Node 22+, no packages.
//   /install          sends the person to Slaiky's install page, with a fresh `state`
//   the redirect URL  checks `state`, then trades the code for that workspace's bot token
import { randomUUID } from 'node:crypto';
import { createServer } from 'node:http';

const APP = process.env.SLAIKY_APP ?? 'https://app.slaiky.xyz';
// One of the app's redirect URLs, exactly as saved on its page.
const REDIRECT = new URL(process.env.SLAIKY_REDIRECT_URL ?? 'http://localhost:3000/slaiky/oauth');
const CLIENT_ID = process.env.SLAIKY_CLIENT_ID;
const SCOPES = 'chat:write,channels:history,app_mentions:read';

const states = new Set(); // install links handed out and not used yet
const tokens = new Map(); // workspace id → its bot token (keep yours in a database)

createServer(async (req, res) => {
  const url = new URL(req.url, REDIRECT);
  if (url.pathname === '/install') {
    const state = randomUUID();
    states.add(state);
    const link = new URL('/oauth/v2/authorize', APP);
    link.search = new URLSearchParams({ client_id: CLIENT_ID, scope: SCOPES, redirect_uri: REDIRECT.href, state });
    return res.writeHead(302, { location: link.href }).end();
  }
  if (url.pathname !== REDIRECT.pathname) return res.writeHead(404).end();

  // Back from Slaiky: ?code=…&state=…, or ?error=access_denied when the person said no.
  const code = url.searchParams.get('code');
  if (!code || !states.delete(url.searchParams.get('state'))) return res.writeHead(400).end('Not installed.');
  const answer = await fetch(`${APP}/api/methods/oauth.v2.access`, {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ clientId: CLIENT_ID, clientSecret: process.env.SLAIKY_CLIENT_SECRET, code, redirectUri: REDIRECT.href }),
  }).then((r) => r.json());
  if (!answer.ok) return res.writeHead(400).end(answer.error.message);
  tokens.set(answer.team.id, answer.accessToken);
  res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' }).end(`Installed in ${answer.team.name}.`);
}).listen(Number(REDIRECT.port) || 3000);
```

## One server for every install

Over HTTP, every install sends to your app’s request URLs, signed with your app’s one signing secret. Each event, command and click names where it comes from: `workspaceId`, and the install’s `appId`. Answer with the token you kept for that workspace.

*Slaiky → your app · `event_callback`*

```json
{
  "type": "event_callback",
  "eventId": "…",
  "eventTime": 1794312000,
  "appId": "…",
  "workspaceId": "…",
  "event": { "type": "app_mention", "channelId": "…", "channelType": "channel", "threadId": null, "message": { "id": "…", "body": "@deploybot hello" } }
}
```

Over Socket Mode, each install’s bot has its own connection: open one with each workspace’s token (`apps.connections.open`). See [Socket Mode](/docs/socket-mode).

## What an install copies

| What | In the install |
| --- | --- |
| Bot, token and permissions | Its own. The permissions are the ones granted there, within the app’s. |
| Name, descriptions, color, request URLs, events, Socket Mode, App Home, shortcuts, signing secret | Follow your app. A change on your app’s page or manifest reaches every install at once. |
| Permissions you take away | Taken away from every install too. Permissions you add are not: people install again to grant them. |
| Slash commands | Copied when it is installed, where the name is free in that workspace. Commands added later don’t reach existing installs. |
| Incoming webhooks, redirect URLs, client id and secret | Not copied. An install can’t be installed elsewhere in turn. |

An install’s settings are changed only where the app was made; its page in the other workspace says so. There, its token, its slash commands and new webhooks can’t be changed either: those calls answer `403`. Your server gets a new token when the app is installed again.

## Uninstalls

Whoever installed it, or an owner or admin of that workspace, can press **Uninstall** on its page there. Its bot is turned off, its commands go, and its token stops: calls with it answer `401` with `unauthorized`. Your app gets `app_uninstalled`, whether it asked for that event or not:

*Slaiky → your app · `event_callback`*

```json
{
  "type": "event_callback",
  "eventId": "…",
  "eventTime": 1794312000,
  "appId": "…",
  "workspaceId": "…",
  "event": { "type": "app_uninstalled", "workspaceId": "…", "byUserId": "…" }
}
```

Removing your app itself removes every install; each one sends `app_uninstalled` with `byUserId: null`.

> **Careful:** `app_uninstalled` is sent once, as the install goes; nothing is sent again after it. If your server missed it, a token that starts answering `unauthorized` tells you the same thing: forget that workspace.
