# Receive Webhooks from Any Service in Type.com

> A Type.com webhook example. A store sends each new order to a Type.com webhook URL. Code skips small orders and posts large ones in the Space.

Canonical page: https://docs.type.com/automations/examples/webhook-orders/

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

```mermaid
flowchart LR
  order(["order · webhook"])
  checkOrder["checkOrder · code"]
  order -->|entry| checkOrder
```

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

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](https://docs.type.com/automations/webhooks/index.md).

## The code

```ts title="src/main.ts"

// 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

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.

## Send a test event

```sh
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

```json title="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" }
  }
]
```

## 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

- [Webhooks](https://docs.type.com/automations/webhooks/index.md): signatures, `GET` requests, limits, and webhook adapters.
