Skip to main content
In this tutorial you build a procedure for an insured’s request to cancel a policy. It reads the policy from your policy system, works out the pro rata return premium, and asks an operations approver to approve the refund. On approval it writes the cancellation to your policy system, refunds the premium through your payment processor and sends the cancellation notice on the cancellation date. If the approver rejects, nothing is written.

What this does for your team

An insured asks to cancel. The procedure checks that the policy can be cancelled and does the refund arithmetic. It then puts the request, the dates and the amount in front of an approver. Nothing changes in your policy system and no money moves until they approve. After approval it cancels the policy, refunds the premium and sends the notice on the cancellation date, without anyone having to remember the date. The run keeps a record of what it read, the amount, who approved it, and the IDs your systems gave the cancellation, refund and notice. If the same request is run twice, your systems answer with what they already did instead of cancelling, refunding or mailing again, as long as they honour the keys the procedure sends. The example carrier is Northwind Mutual. Its hosts, policy numbers, amounts and IDs are made up; replace them with your own.

Before you start

You need:
  • An API key for a member who is not read-only, and curl and jq. See Run procedures from your system, which also covers publishing and starting runs. A draft also needs permission to create procedures, or to publish this one once it exists. Resource-scoped and app API keys can’t start drafts.
  • A group in Effective for the people who approve refunds. The procedure assigns its task to group:operations-approvers; replace that with your group.
  • HTTPS APIs for your policy system and payment processor that accept an Idempotency-Key header on writes and answer a repeated key with their first result. Your policy system must also record which request cancelled a policy. The procedure’s code calls these endpoints; change the hosts, paths and field names to match yours:

The procedure

Save this as cancel-a-policy.procedure.md. The sections after it walk through each part.
cancel-a-policy.procedure.md

How it works

Inputs and output

The front matter declares four inputs. request_id is your own ID for this cancellation request, such as the case number your intake system gave it. The procedure builds its write keys from it, so use a value that is unique to the request and stays the same if the request is sent again. cancellation_date is a YYYY-MM-DD date. The output, cancellation, follows the cancellation-result schema. All three endings return it: status is cancelled, rejected or not-cancellable. The IDs of what was written appear only on the cancelled path, and policyStatus only on the not-cancellable path. run.title names each run after its policy and request, so runs are easy to find.

Read the policy

The read-policy node is a call to the local effect get-policy, declared under Code. A ts effect is TypeScript you write and publish with the procedure. This one may make HTTPS requests with fetch and read one secret, policy-system-token, because its header says uses=fetch,secrets secrets=policy-system-token. The secret’s value is never in the source; you set it in Effective after you publish. See Functions and effects and secret. The effect checks the shape of the response before it returns it, because the result must match the policy schema. A 4xx answer other than 429 throws FatalError: another attempt can’t change it, so the run fails at once. A 429, a 5xx answer or a network error throws an ordinary error, which is retried. The effect also returns the policy’s standing for this request, worked out from status and cancellationRequestId: check-policy is a choice on standing. The first two cases continue to the rest of the procedure. not-cancellable runs Not cancellable, which completes the run with status: not-cancellable and the policy’s status, before anyone is asked to approve anything. Returning the standing as data, instead of failing the run, gives your system a typed answer it can act on, and lets a second run for the same request get past this step. See If a run fails partway.

Work out the return premium

compute-return-premium calls pro-rata-return, a ts function. A function is pure: it gets only its input, with no network, secrets or clock. The same input always gives the same result, so the step is safe to compute again. A pure function can’t use Date, so it counts days with calendar arithmetic. Pro rata means the insured gets back the share of the written premium for the days left in the term after the cancellation date:
For the illustrative policy, the term runs from 2026-03-01 to 2027-03-01 and the written premium is $1,840.00. Cancelled from 2026-12-15:
The function works in whole cents and rounds once, at the end. It returns the amounts as cents for the writes, and as decimal text such as 383.12 for the approver to read. It also returns noticeAt, the time to send the notice: 14:00 UTC on the cancellation date. If your carrier uses short-rate tables or a minimum earned premium, change this function; the rest of the procedure doesn’t change. A cancellation date before the term starts or after it ends makes the function throw. A function’s error is final, so the run fails at compute-return-premium, before anyone is asked to approve, and its failure doesn’t say which date was wrong. Check the date in your own system before you start the run.

Approve the refund

approve-refund is a task. It creates a task for the operations-approvers group and the run waits, without holding anything, until someone in that group answers. The title and instructions fill in the policy’s facts and the arithmetic with {{ ... }}, so the approver sees what they’re approving. The answer must match the refund-decision schema: approved, and an optional note. read-decision is a value node. It copies the answer and turns a missing note into an empty string with ??, so later steps can rely on decision.note.

Approved or rejected

route-decision is a choice on decision.approved. A choice has one case for each value: here 'true' and 'false'. Each case names a heading directly under the choice’s section, and that heading’s nodes run for that case.
  • Rejected. The rejected node completes the run with status: rejected. The run writes nothing to your systems. On a first run the policy stays in force; for a run that follows a failed one, see If a run fails partway.
  • Approved. The run continues under Cancel and refund.
Both cases end in a complete node, so the choice needs nothing after it.

Cancel, refund and notify, each with a key

The approved path makes three writes, each in its own call: write-cancellation, issue-refund and send-notice. Separate nodes give each write its own recorded result. If the refund fails after the cancellation succeeded, the run’s record shows exactly which write happened. Each effect sends a key built from the request ID:
Your system’s handling of that key is what keeps a repeated write from applying twice. Here is what happens when a reply is lost:
  1. create-refund sends the refund with Idempotency-Key: refund-CR-20931. The processor makes the refund, but the connection drops before the reply arrives.
  2. fetch throws, so the attempt fails. Effects get three attempts by default, with a short backoff between them.
  3. The second attempt sends the same request with the same key. The processor recognizes the key and returns the refund it already made. There is one refund.
  4. The run records that result and moves on. If the run is interrupted later and resumes, issue-refund doesn’t run again: the run uses the result it recorded.
Retries alone don’t prevent a duplicate: they bound how many times the effect runs. A retry repeats the request; only the receiving system’s key handling stops the repeat from refunding twice. If one of your endpoints doesn’t accept an idempotency key, a retry policy with maximum_attempts: 1 stops the effect from retrying within one call, and a failure there becomes something a person checks. It doesn’t help across runs: a second run for the same request makes its own first attempt. For that, the receiving system has to deduplicate, or the procedure has to look for the earlier write by a business key, such as the request ID, before it writes. See Writes to your systems. The keys come from the request, not from the run. The context’s operation.idempotencyKey is also stable across every attempt of one call, but it differs between runs. A key derived from the request is the same in every run for that request, so if a second run for the same request gets past check-policy, your systems answer it with the cancellation, refund and notice they already made, for as long as they keep the key.

Send the notice on the cancellation date

wait-for-notice-date is a wait with until. The run sleeps until quote.noticeAt and then continues; it holds nothing while it waits. A date in the past ends the wait at once, so a backdated cancellation sends its notice straight away. Then send-notice sends the notice with its own key, notice-CR-20931. To send the notice as soon as the refund is issued instead, delete the wait node.

Finish

The cancelled node completes the run. Its value must match cancellation-result, which the procedure checks when you publish it and again when the run completes.

If a run fails partway

A run can fail after some writes and before others: for example, the payment processor refuses the refund after the cancellation was written. The run ends failed, and the cancellation stays written; failing the run doesn’t undo it. There is no step that catches the failure. A failed run’s status is final, and the API has no way to resume it. The run’s failure names the step that failed, issue-refund, and its code, but not the message the effect threw: Effective doesn’t show thrown messages, because they could hold secret values. Look in the processor’s own logs for why it refused. A refusal the procedure has to act on belongs in a typed result instead, as get-policy returns standing. To recover, fix what made the refund fail and start a new run with the same inputs, including the same request_id. The new run:
  1. Reads the policy. It is now cancelled, with cancellationRequestId set to this request, so its standing is cancelled-by-this-request and the run goes on.
  2. Works out the same return premium from the same term and premium, and asks for approval again. The task shows the policy status, so the approver can see it is already cancelled. The amount was approved in the failed run, so approve it again to finish. A rejection ends the run rejected and writes nothing more, but the cancellation the failed run wrote stays. The policy is then cancelled with no refund, until you reverse the cancellation in your policy system or approve another run for the request.
  3. Sends the cancellation with cancel-CR-20931 again. Your policy system honours the key, so it answers with the cancellation it already made, and nothing is written twice.
  4. Sends the refund and the notice, which the failed run never made.
Start the new run with its own run Idempotency-Key, such as cancel-CR-20931-2. The run key only deduplicates the start request: the same key as the failed run’s start returns that failed run instead of starting a new one. In the Effective app, Retry on the failed run’s page also starts a new run, from a checkpoint of the failed run that you pick. The page shows what starts again from there, and each call from that point on runs again. The keys above make your systems answer a write they already applied with its first result.

Try it as a draft

A draft runs source without publishing it. See Try a draft. A draft can’t declare secrets, and this procedure declares two, so sending cancel-a-policy.procedure.md as a draft is refused with 422 invalid_procedure and no run starts. To try the flow before you connect your systems, make a stand-in copy that runs without secrets. Save it as cancel-a-policy.draft.procedure.md and change three things:
  1. Delete the two yaml secret blocks under Secrets.
  2. Replace each ts effect with one that returns sample data and declares no uses= or secrets=. For example:
    effect id=get-policy input=policy-request result=policy
    effect id=create-refund input=refund-request result=refund-record
    Do the same for create-cancellation and create-notice, returning a cancellationId and a noticeId.
  3. Assign the task to yourself, so you can answer it:
Keep the function and everything else as it is. Then start the draft:
An illustrative response, abbreviated; your IDs differ:
The run stops at the approval task. Find it with GET /procedures/tasks?status=open&assignedToMe=true, read it to check the title and instructions, and answer it as in Answer a task:
Use the task ID from your own task list. If the cancellation date is still in the future, the run then waits until 14:00 UTC that day before it sends the notice, and stays running until then. Use a past date to see it finish straight away; the return premium changes with the date. Each edit to the draft needs a new Idempotency-Key.

Check the result

Read the run with GET /procedures/runs/{runId} until status is completed. For the inputs above and an approval, output looks like this (illustrative):
Check that:
  • returnPremiumCents is 38312, the $383.12 worked out above. Open the run’s webUrl to see compute-return-premium’s full result, with termDays: 365 and unearnedDays: 76.
  • tasks lists the approval task as completed.
  • A rejected draft (answer { "approved": false }) completes with status: "rejected", an empty approvalNote, and no cancellationId, refundId or noticeId.
  • With status: 'lapsed' and standing: 'not-cancellable' in the stand-in get-policy, the run completes with status: "not-cancellable" and policyStatus: "lapsed", and creates no task.

Run it against your systems

When the flow does what you want, publish the real cancel-a-policy.procedure.md, as in Publish a version. Reading the version lists the two secrets it needs under secrets. Before the first run, set their values on the procedure’s page in the Effective app, under Secrets: a shared default for every run, or a personal override for the runs that run as you. See Give a secret a value. Try it on a test policy first. If your policy system and payment processor have test environments, publish a copy with its own id, such as cancel-a-policy-test, whose effects call your test hosts, and give that copy test credentials. Secrets are set for each procedure, so the copy’s values don’t affect the real procedure’s. Then check what only your systems can show:
  • One cancellation, one refund and one notice for the request, with the IDs the run returned.
  • Start a second run with the same request_id and approve it. Its read-policy step should report the standing cancelled-by-this-request, and your systems should answer with the same cancellation, refund and notice IDs; nothing should be written twice. If the run ends not-cancellable, your policy system isn’t returning cancellationRequestId. If an ID differs, that endpoint isn’t honoring the key. A one-attempt retry policy doesn’t help across runs, so before you go live, make the endpoint deduplicate on the key, or have its effect look up the earlier write by request_id before it writes.
  • Start a run with a different request_id for the same policy. It should end not-cancellable without creating a task.
Start live runs from your own system with a run Idempotency-Key derived from the request, such as cancel-CR-20931, as in Start a run.

Next steps

  • Reconcile payments: loop over a day’s payments and hand only the mismatches the procedure can’t explain to a person.
  • Writes to your systems: retries, idempotency keys and what to do when an endpoint has no key.
  • task and choice: every field of the nodes this tutorial uses for the approval.