# Extensions

An extension is a [package](../docs/language.md#packages) of ordinary Mote:
models, `server fn`s, hooks and components that a program takes with one
`use` line. It is written with nothing but the language a program has. It is
shipped beside the compiler, in this folder, and is not inside the binary: a
program that does not take one carries none of it, and a program that does
can read it, copy it, and change it.

| | |
|---|---|
| [usage/](usage/) | What people do in a program, kept in its own database, with an admin panel: active people, the most used screens, actions and calls, what fails, funnels, who came back, and `track`. [docs/usage.md](../docs/usage.md). |
| [usage-forward/](usage-forward/) | The same events, posted as JSON to another tool. |
| [google/](google/) | Continue with Google: an OpenID Connect sign-in into the program's own accounts and sessions, with a button. [docs/google.md](../docs/google.md). |
| [oauth/](oauth/) | Sign-in with GitHub, Microsoft, Apple, Discord, GitLab, LinkedIn, Slack, any OpenID issuer, a connection per customer; tokens kept for providers' APIs; client credentials, device code. [docs/oauth.md](../docs/oauth.md). |
| [jwt/](jwt/) | A public API's door: API keys, tokens, `/.well-known/jwks.json`, rotation. [docs/jwt.md](../docs/jwt.md). |
| [totp/](totp/) | A code from an authenticator app as a second factor, recovery codes, and a QR code writer. [docs/second-factor.md](../docs/second-factor.md). |
| [passkey/](passkey/) | Passkeys, checked on the back end: a way in, and a second factor. [docs/second-factor.md](../docs/second-factor.md). |
| [magic/](magic/) | Signing in by a link or a code in a mail. [docs/magic.md](../docs/magic.md). |
| [stripe/](stripe/) | Payments, billing and Connect with Stripe: Checkout in every mode, the program's plans kept at Stripe from its code, subscriptions changed and previewed, usage on meters, invoices, tax, saved cards, disputes, payouts, sellers' accounts and transfers; every event checked, done once and read fresh, into `StripePlan`, `stripeCan` and the program's hooks; a billing panel. [docs/stripe.md](../docs/stripe.md). |
| [paypal/](paypal/) | Payments and subscriptions with PayPal: orders approved at PayPal and captured once, refunds, subscriptions, payouts, and its webhooks checked by PayPal's verify call and done once. [docs/paypal.md](../docs/paypal.md). |
| [paddle/](paddle/) | Paddle Billing, a merchant of record: transactions and their checkout links, customers, subscriptions (change with proration, pause, resume, cancel), refunds as adjustments, signed webhooks done once. [docs/paddle.md](../docs/paddle.md). |
| [polar/](polar/) | Polar, a merchant of record for software: checkouts and checkout links, customers by the program's account, subscriptions, benefits as what an account may do (`polarCan`), orders, refunds, the portal; Standard Webhooks done once. [docs/polar.md](../docs/polar.md). |
| [mail/](mail/) | Mail through Resend or Cloudflare Email Service, one `mailSend`; a suppression list kept by Resend's events; bodies from Markdown. [docs/mail.md](../docs/mail.md). |
| [replicate/](replicate/) | Models on Replicate: a prediction waited for or streamed, a long run followed by a job and the webhook across restarts, files in and out. [docs/replicate.md](../docs/replicate.md). |
| [s3/](s3/) | Files in any S3-compatible bucket, on the aws extension's SigV4: put, get, list, parts, presigned addresses. [docs/s3.md](../docs/s3.md). |
| [aws/](aws/) | AWS: SigV4 for any service, a role's keys, SQS, DynamoDB, Lambda, SNS, SES, Bedrock, Secrets Manager, SSM, EventBridge. [docs/aws.md](../docs/aws.md). |
| [gcloud/](gcloud/) | Google Cloud and Google's APIs: Cloud Storage, Sheets, Drive, Calendar, Gmail, FCM and APNs, Pub/Sub, BigQuery, Gemini, Maps. [docs/gcloud.md](../docs/gcloud.md). |
| [azure/](azure/) | Azure Blob Storage and Communication Services Email. [docs/azure.md](../docs/azure.md). |
| [ai/](ai/) | Language models (Anthropic, OpenAI's Responses and Chat Completions, Gemini): turns, streaming, tools and answers from the program's own types, batches, files, pictures, speech, costs and budgets, retrieval (chunks, a vector store, Pinecone, Qdrant, reranking), `AiChat`. [docs/ai.md](../docs/ai.md). |
| [twilio/](twilio/) | Text messages and phone codes through Twilio. [docs/twilio.md](../docs/twilio.md). |
| [slack/](slack/) | A Slack app: the Web API (messages, threads, files, people, channels, modals, the home tab), Block Kit as shapes, and events, slash commands and interactivity checked and done in jobs. [docs/slack.md](../docs/slack.md). |
| [notion/](notion/) | Notion: databases' data sources queried and read page by page, every property plain, pages made and changed, search, a page's content as Markdown both ways. [docs/notion.md](../docs/notion.md). |
| [linear/](linear/) | Linear: issues listed by filter and cursor, made and changed, comments, teams and states over its GraphQL API; its webhooks checked and done once. [docs/linear.md](../docs/linear.md). |
| [github/](github/) | GitHub: repositories, files, issues, pull requests, comments, checks and statuses, releases, Actions over REST page by page and GraphQL; a GitHub App's tokens; webhooks checked and done once. [docs/github.md](../docs/github.md). |
| [graphql/](graphql/) | Any GraphQL API: a query and its variables, errors as failures with a kind, a connection page by page. [docs/graphql.md](../docs/graphql.md). |
| [telegram/](telegram/) | A Telegram bot: messages, keyboards, files, presses; updates by webhook or long polling, once. [docs/telegram.md](../docs/telegram.md). |
| [airtable/](airtable/) | Airtable: records listed by formula, view and sort page by page, written ten a request, the base's limit kept, and its changes told by webhooks, once. [docs/airtable.md](../docs/airtable.md). |
| [discord/](discord/) | A Discord bot: messages with embeds and components, channels, members and roles, webhooks, slash commands, the Interactions Endpoint checked with Ed25519; buckets honoured. [docs/discord.md](../docs/discord.md). |
| [mcp/](mcp/) | The Model Context Protocol over Streamable HTTP: the program as an MCP server (typed fns as tools, resources, prompts) and as a client of other servers, whose tools the ai extension's loop can call. [docs/mcp.md](../docs/mcp.md). |
| [alert/](alert/) | A line to Slack, Discord or a webhook; faults once in ten minutes. [docs/alert.md](../docs/alert.md). |
| [sentry/](sentry/) | Faults and reports to Sentry. [docs/sentry.md](../docs/sentry.md). |
| [captcha/](captcha/) | Turnstile or hCaptcha, the server's check. [docs/captcha.md](../docs/captcha.md). |
| [postgres/](postgres/) | Postgres over its own wire protocol, on `tcp.open`: parameters, types, a pool, transactions, LISTEN/NOTIFY, COPY. [docs/postgres.md](../docs/postgres.md). |
| [redis/](redis/) | Redis over RESP (or Upstash's REST API): the common commands typed, pipelines, transactions, pub/sub, streams, a rate limiter, a lock, cache-aside. [docs/redis.md](../docs/redis.md). |
| [admin/](admin/) | The admin page: people and their sessions, every model's rows (changed only as the model's own rules let that admin), jobs, health, access, a log of what admins did, and "see what they see". [docs/admin.md](../docs/admin.md). |

## Taking one

Say where it is in the `mote.toml` beside the program's entry (or above
it), and `use` its file:

```toml
[packages]
usage = "../mote/extensions/usage"
```

```text
use "pkg:usage/usage.mote"
```

The path is a folder on this machine: from the `mote.toml`, or whole. A path
that climbs out of the project (`../../mote/extensions/usage`) is fine.
Nothing is fetched. A program that is deployed from its own repository
copies the folder into it (`cp -r …/extensions/usage vendor/usage`, and
`usage = "vendor/usage"`): it is a few files of source, and then it is the
program's own to keep or change.

A name is declared once in a whole program, so an extension's names all
begin with its own (`Usage…`, `usage…`).

## Writing one

For a service that has an HTTP API and no extension here,
[docs/services.md](../docs/services.md) is the pattern in forty lines,
and a table of what one would npm install and what to use in Mote instead.

- A file of declarations: `model`s (with no rules, so that only its own
  `server fn`s reach them), `server fn`s, `server on …` / `server at` /
  `job fn` blocks, `component`s.
- **What it needs from the program, it says with `wants fn`** at the top of
  its file ([language.md](../docs/language.md#what-a-package-wants-of-its-taker)):
  the usage extension says `wants fn usageMay(): bool`, and until the
  program declares it the build says so, at the program's entry, with the
  line to write. Where what it needs is *who may*, a rule is better: the
  admin extension's `server fn`s say `requires admin`, the program declares
  `rule admin = …`, the refusal is the runtime's, and `mote check --access`
  shows it.
- **What the program may say, and need not**, is a `wants fn` with a
  default: the Google extension says `wants fn googleMay(who: OidcWho): bool
  = true`, and a program's own `googleMay` that gives false (or fails) is a
  no. (Before `wants fn` it was a hook on a model of the extension's, a row
  added and taken away so that a `server before OidcSignIn.add(who)` that
  fails is a no: that hook is still asked, after `googleMay`.)
- **An address of its own** is a `server route` (`GET
  "/auth/google/start"`): its first part is the extension's, so that it
  stands in front of nothing of the program's.
- **A place for the program to add to** is a named slot (`slot
  "admin.tabs" tab=tab`), which any file fills.
- **A file that needs another package** takes it: a package's `use
  "pkg:usage/usage.mote"` is looked up through the program's own
  `[packages]` first, so `admin/usage.mote` takes the usage extension for
  the program, which says where both are.
- **Settings are the back end's surroundings**: `env("USAGE_KEEP_DAYS")`.
- **Its tests are `test` blocks in a file of its own** (`usage/tests.mote`,
  `google/tests.mote`), run by `mote test` from a small program that takes
  the extension (`tests/e2_rollup/`, `tests/google_signin/unit.mote` here).
