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.
- 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, likeapi.stripe.com. The agent says which name and host it needs. - The code uses
env.STRIPE_KEY, which holds the placeholderscava: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. - Calls of the service to the app go to a webhook. 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 |
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:
- 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
authorizationheader tograph.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.