Skip to content
Changelog

Report a bug

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.

TriggerA custom webhook URL (webhook.received)
StagescheckOrder (code)
NeedsA webhook source in the Space, and its source ID (ches_...)
entry order webhook checkOrder code
Trigger Code stage Agent stage

Each node in the graph is one key in the code:

NodeTypeWhat it does
orderTriggerStarts a run for each request to the webhook URL.
checkOrderCode stagePosts large orders in the Space. Completes the run for small orders.

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.

src/main.ts
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 });
},
}),
},
});
  1. The source is an event_source with the webhook’s source ID. Each accepted request becomes one webhook.received event.
  2. schema checks the sender’s JSON in data. A request without orderId, customer, or a numeric amount fails the run before the stage, and Type keeps the event for diagnosis.
  3. mode: "code" says that this automation has no agent stages.
  4. The effect key uses the order ID, so each order has one key.
Terminal window
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.

fixtures.json
[
{
"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" }
}
]
ValueReplace with
ches_exampleYour webhook’s source ID.
ch_exampleThe ID of your Space.
orderReceivedThe fields that your service sends. Use z.looseObject() so new fields do not fail the run.
  • Webhooks: signatures, GET requests, limits, and webhook adapters.