Skip to content
Scava Docs Sign inGet started

Payments open soon. You can create your account today; choosing a plan and connecting an agent open with checkout, and Scava emails you once when it does.

All chapters

Server code

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

An app connects to any service with a web API through its 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. Its signing secret is a readable secret, because the code computes with it.
ServiceBind the key toHow the code sends it
Stripeapi.stripe.comauthorization: 'Bearer ' + env.STRIPE_KEY
Slack (Web API)slack.comauthorization: 'Bearer ' + env.SLACK_TOKEN (the bot token)
Slack (incoming webhook)hooks.slack.comThe address is the key: bind the path part, see below
Notionapi.notion.comauthorization: 'Bearer ' + env.NOTION_KEY, plus notion-version
Airtableapi.airtable.comauthorization: 'Bearer ' + env.AIRTABLE_KEY
HubSpotapi.hubapi.comauthorization: 'Bearer ' + env.HUBSPOT_KEY
Linearapi.linear.appauthorization: env.LINEAR_KEY
GitHubapi.github.comauthorization: 'Bearer ' + env.GITHUB_TOKEN, plus a user-agent
Telegramapi.telegram.orgIn the address: https://api.telegram.org/bot${env.TELEGRAM_TOKEN}/sendMessage
Discorddiscord.comauthorization: 'Bot ' + env.DISCORD_TOKEN
Google APIsoauth2.googleapis.comSign-in with OAuth, see Sign-in with another service

Any other service works the same way. The owner's 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:

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). 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:

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:

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, goes into ctx.waitUntil() or an agent 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 #

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:

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.