Receive Webhooks from Any Service in Type.com
Any service that can send an HTTP request can start an automation. In this example, a store sends each new order to a Type webhook URL. Code skips small orders and posts large ones in the Space.
| Trigger | A custom webhook URL (webhook.received) |
| Stages | checkOrder (code) |
| Needs | A webhook source in the Space, and its source ID (ches_...) |
How it runs
Section titled “How it runs”Each node in the graph is one key in the code:
| Node | Type | What it does |
|---|---|---|
order | Trigger | Starts a run for each request to the webhook URL. |
checkOrder | Code stage | Posts large orders in the Space. Completes the run for small orders. |
Before you start
Section titled “Before you start”Create the webhook in the Space: open Automations → New automation, choose Webhook in Trigger, and select Create automation. Copy the webhook URL. The source ID (ches_...) is in the automation’s code, in source.sourceId. See Webhooks.
The code
Section titled “The code”import { automation, code, complete } from "@type-dot-com/sdk/automations";import { err } from "neverthrow";import { z } from "zod";
// Replace with your webhook's source ID and your Space.const webhookSourceId = "ches_example";const spaceId = "ch_example";
// Orders at or above this amount get a message.const largeOrderAmount = 1000;
// The event that starts a run. The sender's JSON body is in `data`.// Example body: {"orderId":"ord_123","customer":"Acme","amount":2500}const orderReceived = z.object({ type: z.literal("webhook.received"), id: z.string(), resourceType: z.string().nullable(), resourceId: z.string().nullable(), data: z.looseObject({ orderId: z.string(), customer: z.string(), amount: z.number(), }),});
export default automation({ output: z.object({ status: z.enum(["skipped", "posted"]), orderId: z.string(), }),
deployment: { mode: "code", // no agent stages subscriptions: { order: { title: "Large orders", description: "Post each large order in the Space", source: { kind: "event_source", sourceId: webhookSourceId, events: ["webhook.received"], }, schema: orderReceived, entry: "checkOrder", }, }, },
stages: { // Skip small orders. Post large ones. checkOrder: code({ input: orderReceived, transitions: [], async run({ ctx, input: event }) { const order = event.data;
if (order.amount < largeOrderAmount) { return complete({ status: "skipped", orderId: order.orderId }); }
const sent = await ctx.write.sendMessage( { channelId: spaceId, content: `Large order from ${order.customer}: $${order.amount} (${order.orderId})`, }, { effectKey: `large-order:${order.orderId}` }, ); if (sent.isErr()) { return err(sent.error); }
return complete({ status: "posted", orderId: order.orderId }); }, }), },});How it works
Section titled “How it works”- The source is an
event_sourcewith the webhook’s source ID. Each accepted request becomes onewebhook.receivedevent. schemachecks the sender’s JSON indata. A request withoutorderId,customer, or a numericamountfails the run before the stage, and Type keeps the event for diagnosis.mode: "code"says that this automation has no agent stages.- The effect key uses the order ID, so each order has one key.
Send a test event
Section titled “Send a test event”curl --fail-with-body "$TYPE_WEBHOOK_URL" \ -H 'Content-Type: application/json' \ --data-binary '{"orderId":"ord_123","customer":"Acme","amount":2500}'A 200 response with {"success":true} means that Type accepted the request. Read the run history to see what the automation did.
Test it
Section titled “Test it”[ { "name": "skips a small order", "subscriptionId": "order", "payload": { "type": "webhook.received", "id": "delivery-1", "resourceType": "webhook", "resourceId": "webhook_1", "data": { "orderId": "ord_100", "customer": "Acme", "amount": 49 } }, "toolResponses": [], "expectedOutput": { "status": "skipped", "orderId": "ord_100" } }]Make it yours
Section titled “Make it yours”| Value | Replace with |
|---|---|
ches_example | Your webhook’s source ID. |
ch_example | The ID of your Space. |
orderReceived | The fields that your service sends. Use z.looseObject() so new fields do not fail the run. |
Related
Section titled “Related”- Webhooks: signatures,
GETrequests, limits, and webhook adapters.