Skip to main content
NetScriptdocs Search docs ⌘K v0.0.9
Browse documentation
View as Markdown

A verified shipping webhook

In this chapter you accept events from outside your shop. A carrier reports that a parcel shipped by posting a webhook; you add an HMAC-verified webhook endpoint for it that hands each inbound event to a background job. Triggers are how NetScript receives events from external systems.

  1. 1 · Scaffold
  2. 2 · Catalog service
  3. 3 · Cart contracts
  4. 4 · Checkout saga
  5. 5 · Shipping webhook
  6. 6 · Storefront UI
  7. 7 · Deploy

What you will build

A webhook written with defineWebhook(...), HMAC-SHA256 verified against a shared secret, that calls enqueueJob(...) for each accepted request so a worker job processes the shipping update off the request path. At the end you POST a payload to the triggers API and see the inbound event recorded and the job run.

Before you begin

From chapter 4, check that:

  • my-shop/ has the products service, the cart contract, and the sagas plugin with your CheckoutSaga and the process-payment worker job;
  • aspire start is up (the dashboard answers at the URL Aspire prints). The triggers processor and its job hand-off depend on Deno KV and the workers runtime.

Step 1 — Check the triggers plugin

Chapter 4 installed the official triggers plugin with its sample modules. Confirm it is registered:

Shell
netscript plugin list

The list shows workers, sagas, triggers and streams. If triggers is missing, install it from the project root:

Shell
netscript plugin install trigger --name triggers --samples

The plugin is a workspace at plugins/triggers/, registered in netscript.config.ts (./plugins/triggers/mod.ts) and appsettings.json. --samples adds working webhook modules and a small jobs/ folder to study.

Step 2 — Author the shipping-update job and the verified webhook

The webhook hands each inbound event to a background job, so generate that job first.

Shell
ns-workers add job process-shipping-update

Extend the generated payload schema with orderId, status and trackingNumber, then replace the starter handler body with the validation and application logic. A real integration would advance the order; this one validates and records the update:

workers/jobs/process-shipping-update.tsTypeScript
// workers/jobs/process-shipping-update.ts
import { createSuccessResult, defineJobHandler } from '@netscript/plugin-workers-core';
import { z } from 'zod';

const PayloadSchema = z.object({
  orderId: z.string().min(1),
  status: z.string().min(1),
  trackingNumber: z.string().optional(),
});

const handler = defineJobHandler(async (ctx) => {
  const update = PayloadSchema.parse(ctx.payload ?? {});
  // ... advance the order / notify the customer ...
  return createSuccessResult(update);
});

export default Object.assign(handler, { id: 'process-shipping-update' });

Then generate the webhook and its worker-job action with the triggers CLI. The CLI's add-webhook verb is written as add webhook on the command line:

Shell
ns-triggers add webhook shipping-status-webhook \
  --path=shipping/status \
  --job=process-shipping-update \
  --verifier=hmac-sha256 \
  --secret-env=WEBHOOK_SHIPPING_SECRET \
  --description="Receives carrier shipping-status callbacks and enqueues a processing job." \
  --tags=webhook,shipping,saga

The command writes triggers/shipping-status-webhook-trigger.ts and recompiles the generated trigger registry. The definition uses defineWebhook(handler, spec) from @netscript/plugin-triggers-core/builders: the handler resolves to an array of effects, and the spec names the webhook and declares HMAC verification, so forged requests are rejected before your handler runs. The job reference is an inline object typed with satisfies JobDefinition. The relevant shape:

triggers/shipping-status-webhook-trigger.tsTypeScript
// triggers/shipping-status-webhook-trigger.ts
import { defineWebhook, enqueueJob } from '@netscript/plugin-triggers-core/builders';
import type { JobDefinition } from '@netscript/plugin-workers-core';

// A typed reference to the worker job authored above.
const processShippingJob = {
  id: 'process-shipping-update' as JobDefinition<'process-shipping-update'>['id'],
  name: 'Process Shipping Update',
  topic: 'default',
} satisfies JobDefinition<'process-shipping-update'>;

export default defineWebhook(
  // The handler returns effects. Here: enqueue one job with the inbound payload.
  (event) => Promise.resolve([enqueueJob(processShippingJob, { payload: event.payload })]),
  {
    id: 'shipping-status-webhook',
    path: 'shipping/status',
    verifier: 'hmac-sha256',
    secretEnv: 'WEBHOOK_SHIPPING_SECRET',
    description: 'Receives carrier shipping-status callbacks and enqueues a processing job.',
    tags: ['webhook', 'shipping', 'saga'],
    metadata: {
      direction: 'inbound',
      pipeline: 'shipment-fulfillment',
      provider: 'carrier',
    },
  },
);

Three things to notice:

  • defineWebhook(handler, spec) takes the handler first, then the static spec. The handler resolves to an array of effects: return a Promise of the effect array, never a bare function-keyword declaration.
  • verifier: 'hmac-sha256' with secretEnv is the security seam. The triggers ingress checks the request's HMAC signature against the secret named by secretEnv (here WEBHOOK_SHIPPING_SECRET) before it invokes your handler, so a request that fails verification never reaches your code.
  • enqueueJob(jobRef, { payload }) is the effect that bridges to the worker system. Effects run after the request is accepted; this one enqueues process-shipping-update with the verified inbound payload.
defineWebhook spec fields
Name · TypeDescription
idstring Stable identifier for the webhook in the registry.
pathstring URL segment the router mounts it under — here shipping/status, reached at /api/v1/webhooks/shipping/status.
verifier'hmac-sha256' | 'memory' | string Signature verifier. hmac-sha256 checks the request HMAC against secretEnv; memory is the open local-dev verifier.
secretEnvstring (optional) Name of the env var holding the shared secret used by the hmac-sha256 verifier.
tags / metadataoptional Discovery metadata — not behavior. Useful for grouping and dashboards.

Step 3 — Choose immediate work or one-shot replay

A handler returns an array of trigger actions (effects). The runtime dispatches these two:

Trigger actions
Name · TypeDescription
enqueueJob(jobRef, opts)Live Places a worker job on the queue. The supported way to turn an inbound event into durable background work.
defer({ until })Live Persists the event and replays its handler once at or after the ISO timestamp, surviving runtime restarts.

Use enqueueJob(...) when the work is ready now, and defer({ until }) when the same event should be processed once more at a future instant. Deferred delivery is at-least-once, so keep the handler idempotent. For recurring work, use cron scheduling.

Step 4 — Route shape (raw webhook ingress)

The triggers service serves a typed v1 oRPC contract for trigger and event introspection and management, but the webhook ingress endpoint is a raw route, not an oRPC procedure. The sender is a third party with no NetScript client, posting plain JSON to a fixed path, and the endpoint verifies an HMAC signature over the raw request bytes, so a typed contract adds nothing.

The ingress handler resolves a POST /:triggerId path: a request to /api/v1/webhooks/shipping/status resolves :triggerId to shipping/status, which matches your webhook's path, so the handler runs and its effects are applied. See the triggers capability for verifiers, scheduling and file-watch triggers.

Step 5 — Set the secret and start the triggers service

The hmac-sha256 verifier reads its secret from the environment. Set it before you start the service, and use the same value when you sign your test request:

Shell
export WEBHOOK_SHIPPING_SECRET=dev-shipping-secret

Under aspire start, Aspire runs the triggers API and its background processor: look for the triggers-api and triggers resources in the dashboard at the URL Aspire prints. To run the API on its own during development, start it from the plugin workspace:

Shell
deno task --cwd plugins/triggers dev

The triggers API listens on a host port Aspire allocates at start. Read the triggers-api endpoint from the Aspire dashboard resource list and confirm it is alive:

Shell
curl <triggers-endpoint>/health

The commands below write <triggers-endpoint> for that value.

Verify your progress

Because the webhook is hmac-sha256-verified, a real sender includes an x-hub-signature-256 header: a hex HMAC-SHA256 of the raw body under the shared secret, optionally prefixed with sha256=. The carrier's dashboard computes it for you.

  1. Send an inbound request to your webhook's path, with verification satisfied:

    Shell
    curl -X POST <triggers-endpoint>/api/v1/webhooks/shipping/status \
      -H "content-type: application/json" \
      -d '{"orderId":"ord_1001","status":"shipped","trackingNumber":"1Z999"}'
    

    An accepted request returns 202. The request resolves the trigger id shipping/status, your handler runs, and its enqueueJob(...) effect places process-shipping-update on the workers queue.

  2. Confirm both sides of the hand-off:

    Shell
    # 1. The trigger recorded the inbound event (Hono ingress)
    deno x -A jsr:@netscript/plugin-triggers@0.0.9/cli events --limit=10 --json
    
    # 2. The worker job it enqueued has executed (workers API; the CLI resolves it)
    ns-workers executions --limit=10 --json
    

    The triggers events endpoint lists the inbound event, and the workers executions list shows a new execution of process-shipping-update.

  • [ ] netscript plugin list shows triggers, installed at plugins/triggers/.
  • [ ] triggers/shipping-status-webhook-trigger.ts uses verifier: 'hmac-sha256' with a secretEnv.
  • [ ] WEBHOOK_SHIPPING_SECRET is set in the environment running the triggers service.
  • [ ] curl <triggers-endpoint>/health answers.
  • [ ] A verified POST records an event on the triggers API and produces a worker execution.

What you built

An HMAC-SHA256 verified webhook, written with defineWebhook(handler, { id, path, verifier, secretEnv, ... }), that rejects forged requests and enqueues a process-shipping-update job for each accepted one. You confirmed the hand-off end to end: an event recorded on the triggers API and a worker execution on the workers API. The backend now covers catalog, cart, durable checkout and verified ingress. Next, you give it a storefront page.

Search all documentation

Type a term to search the documentation.