Skip to Content
ExamplesSubscription save

Subscription save

Code  · Template: Subscription save agent · Runs on http://localhost:3012

A mock subscription store. Ask the agent to cancel as a test subscriber, watch the outcome arrive at the store’s webhook, then confirm the cancellation from the store desk.

A subscriber who cancelled, with the chat, the last cancel request, the cancellation and the timeline

The store deskSubscribers
Outcomes from the webhook, with one cancellation confirmedSubscribers and how their last request ended

What it shows

  • Receiving a workflow’s webhook. subscription-save-outcome posts each outcome to the app. The request isn’t signed, so the receiver checks a secret key in the URL, and saves each outcome once even if Loops retries.
  • A store applying the outcome. An outcome of cancelled doesn’t cancel anything. The store desk confirms it by triggering subscription-cancelled, which records it and can send a win-back SMS.
  • Telling the agent the plan. The agent can’t read the store, so the first message of each thread says which subscription the person has.

The endpoints it uses

EndpointUsed for
POST /chat on subscription-saveSending one message and getting the reply.
POST / on subscription-cancelledConfirming a cancellation from the store desk.
GET /usersThe subscriber list, 50 at a time with Load more.
GET /users/:userIdA subscriber’s last cancel request, cancellation and memory.

Run it

  1. Deploy the Subscription save agent template and tick the optional subscription-cancelled workflow.
  2. Copy the organization id, the subscription-save loop id and the subscription-cancelled loop id.
  3. In Settings → API tokens, create a token for the whole organization with access set to Run and read.
  4. Copy apps/subscription-save/.env.example to .env and fill in LOOPS_API_URL, LOOPS_ORGANIZATION_ID, LOOPS_AGENT_LOOP_ID, LOOPS_CANCELLED_LOOP_ID and LOOPS_API_TOKEN. Set SUBSCRIPTION_WEBHOOK_KEY to a long random string.
  5. On subscription-save-outcome, set SUBSCRIPTION_WEBHOOK_URL under Variables and secrets to http://localhost:3012/api/subscription-outcome?key=<your key>, and add the app’s host to its allowed hosts.
  6. Run bun run dev --filter=subscription-save.

If Loops doesn’t run on your machine, the webhook URL has to be one it can reach, for example through a tunnel. Set RESTART_URL on subscription-cancelled to put a link in the win-back SMS.

Where to look in the code

FileWhat it does
src/lib/loops.tsServer functions for the Loops endpoints.
src/lib/subscribers.tsWhat Loops doesn’t store: subscribers, their plans, threads and chats.
src/routes/api/subscription-outcome.tsThe webhook receiver.
src/lib/outcomes.server.tsEvery outcome received, kept in .data/outcomes.json.
src/routes/customers.$customerId.tsxA subscriber’s cancel chat and what Loops knows about them.
src/routes/desk.tsxThe store desk: outcomes to apply, and cancellations to confirm.
Last updated on