# Connect Stripe, Slack, Notion and other services to an app

An app connects to any service with a web API through its [server code](/docs/server-code). There is no list to choose from and nothing to install: the code calls the service with `fetch()`, and the key stays with Scava.

1. The owner sets the key on the app page at scava.ai under "Server code": a name like `STRIPE_KEY`, the key, and the host it is for, like `api.stripe.com`. The agent says which name and host it needs.
2. The code uses `env.STRIPE_KEY`, which holds the placeholder `scava:key:STRIPE_KEY`, in the header or the address the service expects. Scava puts the real key in on the way out, only for that host.
3. Calls of the service to the app go to a [webhook](/docs/schedules-webhooks#webhooks). Its signing secret is a readable secret, because the code computes with it.

| Service | Bind the key to | How the code sends it |
|---|---|---|
| Stripe | `api.stripe.com` | `authorization: 'Bearer ' + env.STRIPE_KEY` |
| Slack (Web API) | `slack.com` | `authorization: 'Bearer ' + env.SLACK_TOKEN` (the bot token) |
| Slack (incoming webhook) | `hooks.slack.com` | The address is the key: bind the path part, see below |
| Notion | `api.notion.com` | `authorization: 'Bearer ' + env.NOTION_KEY`, plus `notion-version` |
| Airtable | `api.airtable.com` | `authorization: 'Bearer ' + env.AIRTABLE_KEY` |
| HubSpot | `api.hubapi.com` | `authorization: 'Bearer ' + env.HUBSPOT_KEY` |
| Linear | `api.linear.app` | `authorization: env.LINEAR_KEY` |
| GitHub | `api.github.com` | `authorization: 'Bearer ' + env.GITHUB_TOKEN`, plus a `user-agent` |
| Telegram | `api.telegram.org` | In the address: `https://api.telegram.org/bot${env.TELEGRAM_TOKEN}/sendMessage` |
| Discord | `discord.com` | `authorization: 'Bot ' + env.DISCORD_TOKEN` |
| Google APIs | `oauth2.googleapis.com` | Sign-in with OAuth, see [Sign-in with another service](/docs/migrate#sign-in-with-another-service) |

Any other service works the same way. The owner's [house rules](/docs/house-rules) can limit which services the apps may call.

## Stripe: payments

A payment with Stripe Checkout needs no script of Stripe in the page. The server code creates the session and the page goes there:

```js
export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    if (url.pathname === '/api/pay' && request.method === 'POST') {
      const form = new URLSearchParams({
        mode: 'payment',
        'line_items[0][price]': 'price_123',
        'line_items[0][quantity]': '1',
        success_url: url.origin + '/thanks',
        cancel_url: url.origin + '/',
      });
      const res = await fetch('https://api.stripe.com/v1/checkout/sessions', {
        method: 'POST',
        headers: { authorization: 'Bearer ' + env.STRIPE_KEY, 'content-type': 'application/x-www-form-urlencoded' },
        body: form,
      });
      const session = await res.json();
      return Response.json({ url: session.url });
    }
    // Stripe tells the app about the payment: webhook "stripe" in scava.json.
    if (url.pathname === '/_scava/hook/stripe') {
      const payload = await request.text();
      // Check request.headers.get('stripe-signature') with env.STRIPE_SIGNING (a readable secret).
      const event = JSON.parse(payload);
      if (event.type === 'checkout.session.completed') await env.SCAVA.db('payments').add({ session: event.data.object.id });
      return new Response('ok');
    }
    return new Response('Not found', { status: 404 });
  },
};
```

The page then opens `session.url`. A payment form inside the page (Stripe Elements) needs Stripe's script: list it in `scava.json` with `"embeds": ["js.stripe.com", "*.stripe.com"]` (see [Other sites in the page](/docs/hosting#other-sites-in-the-page)). The publishable key (`pk_...`) may be in the page; the secret key (`sk_...`) never, the upload refuses it.

## Slack: messages and a bot

Sending a message to a channel:

```js
await fetch('https://slack.com/api/chat.postMessage', {
  method: 'POST',
  headers: { authorization: 'Bearer ' + env.SLACK_TOKEN, 'content-type': 'application/json' },
  body: JSON.stringify({ channel: 'C0123456789', text: 'New vacation request from Anna' }),
});
```

An incoming webhook of Slack is an address that is the key itself. Bind it to `hooks.slack.com` and put the placeholder into the address: the secret is the path after `https://hooks.slack.com/services/`, for example `T000/B000/XXXX`, and the code calls `'https://hooks.slack.com/services/' + env.SLACK_HOOK`.

A bot that answers in Slack uses the Events API: in the Slack app settings the request URL is the address of a webhook of the app, such as `https://<app address>/_scava/hook/slack?key=...`. Slack first checks the address with a challenge, which the code sends back:

```js
if (url.pathname === '/_scava/hook/slack') {
  const body = await request.json();
  if (body.type === 'url_verification') return Response.json({ challenge: body.challenge });
  // Check x-slack-signature with env.SLACK_SIGNING_SECRET (a readable secret), then act on body.event.
  return new Response('ok');
}
```

Slack expects an answer within three seconds. Longer work, such as asking an [AI](/docs/server-code#ai), goes into `ctx.waitUntil()` or an [agent](/docs/app-agents) that wakes a second later. Slack's Socket Mode, where the app keeps a connection open to Slack, does not work on Scava; the Events API does the same.

## Notion: reading and writing pages

```js
const res = await fetch('https://api.notion.com/v1/pages', {
  method: 'POST',
  headers: { authorization: 'Bearer ' + env.NOTION_KEY, 'notion-version': '2022-06-28', 'content-type': 'application/json' },
  body: JSON.stringify({ parent: { database_id: 'abc123' }, properties: { Name: { title: [{ text: { content: 'From the app' } }] } } }),
});
```

The key is the token of a Notion integration that the database is shared with.

## Telegram, WhatsApp and Discord bots

These services call a webhook of the app for every message, and the code answers with a call back:

- **Telegram:** register the webhook address with `setWebhook`; the bot token goes into the address of each call (`/bot<token>/...`), which a bound key allows.
- **WhatsApp Business:** the webhook gets the messages; the access token goes in the `authorization` header to `graph.facebook.com`.
- **Discord:** slash commands and buttons come to the interactions endpoint, which is a webhook of the app; check the signature with the public key of the Discord app (not a secret). Reading every message of a server needs Discord's gateway, a connection the app would keep open, which Scava does not offer.

## Limits

Each request of the server code may make a number of outside calls, depending on the plan, and every call is counted under "talks to" on the app page. The numbers are in [Limits](/docs/limits).
