# Automation inbound webhooks

Start an automation flow from another system with one signed call. The flow uses the fields of the call as variables.

> **Off by default**
>
> Inbound webhooks are off for every tenant until an Admin turns them on (Administration › Automation, with a fresh sign-in). Then an Admin registers an endpoint. An endpoint is closed to project flows until an Admin opens it.

1. **An Admin registers an endpoint**

   In **Administration › Automation › Inbound**, the Admin creates an endpoint. The answer shows its secret, `whsec_…` (32 random bytes), one time. The Admin can also limit the sender's address ranges.

   A rotation shows a new secret one time and keeps the old one for 24 hours.

2. **A flow uses the endpoint**

   A flow editor selects the trigger **Inbound webhook**, names the endpoint and defines the fields. The fields come from a defined JSON structure, or an AI converter fills the same fields from the body.

3. **Your system signs and sends the call**

   Send a `POST` with a JSON body (at most 64 KiB, depth at most 16, at most 500 keys) and the Standard Webhooks headers. Make a new `webhook-id` per event and keep it for the retries of that event.

   *Sign and send a call*

   **curl**

   ```bash
   SECRET="whsec_…"                          # from the endpoint, shown one time
   URL="https://app.youniton.example/webhooks/automation/<endpointId>"
   ID="evt_$(uuidgen)"
   TS=$(date +%s)
   BODY='{"orderId":"A-1042","status":"paid"}'
   KEY_HEX=$(printf '%s' "${SECRET#whsec_}" | base64 -d | xxd -p -c 256)
   SIG=$(printf '%s' "$ID.$TS.$BODY" | openssl dgst -sha256 -mac HMAC -macopt hexkey:$KEY_HEX -binary | base64)

   curl -s -X POST "$URL" \
     -H "Content-Type: application/json" \
     -H "webhook-id: $ID" -H "webhook-timestamp: $TS" -H "webhook-signature: v1,$SIG" \
     --data "$BODY"
   ```

   **Node**

   ```js
   import { createHmac, randomUUID } from "node:crypto";

   export async function sendToYouniton(url, secret, payload, id = `evt_${randomUUID()}`) {
     const body = JSON.stringify(payload);
     const timestamp = Math.floor(Date.now() / 1000).toString();
     const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
     const signature = createHmac("sha256", key).update(`${id}.${timestamp}.${body}`).digest("base64");
     const res = await fetch(url, {
       method: "POST",
       headers: {
         "Content-Type": "application/json",
         "webhook-id": id,
         "webhook-timestamp": timestamp,
         "webhook-signature": `v1,${signature}`,
       },
       body,
     });
     return res.status; // 202 accepted · 200 a repeat of the same id and body
   }
   ```

   **Python**

   ```python
   import base64, hashlib, hmac, json, time, uuid, requests

   def send_to_youniton(url, secret, payload, msg_id=None):
       msg_id = msg_id or f"evt_{uuid.uuid4()}"
       body = json.dumps(payload, separators=(",", ":"))
       timestamp = str(int(time.time()))
       key = base64.b64decode(secret.removeprefix("whsec_"))
       sig = base64.b64encode(hmac.new(key, f"{msg_id}.{timestamp}.{body}".encode(), hashlib.sha256).digest()).decode()
       return requests.post(url, data=body, headers={
           "Content-Type": "application/json",
           "webhook-id": msg_id,
           "webhook-timestamp": timestamp,
           "webhook-signature": f"v1,{sig}",
       }).status_code
   ```

   **PHP**

   ```php
   <?php
   function sendToYouniton(string $url, string $secret, array $payload, ?string $id = null): int
   {
       $id ??= 'evt_' . bin2hex(random_bytes(16));
       $body = json_encode($payload, JSON_UNESCAPED_SLASHES);
       $timestamp = (string) time();
       $key = base64_decode(preg_replace('/^whsec_/', '', $secret));
       $signature = base64_encode(hash_hmac('sha256', "{$id}.{$timestamp}.{$body}", $key, true));
       $res = (new GuzzleHttp\Client())->post($url, [
           'headers' => [
               'Content-Type' => 'application/json',
               'webhook-id' => $id,
               'webhook-timestamp' => $timestamp,
               'webhook-signature' => "v1,{$signature}",
           ],
           'body' => $body,
           'http_errors' => false,
       ]);
       return $res->getStatusCode();
   }
   ```

4. **Read the answer**

   | Answer | Meaning |
   |---|---|
   | `202` | Youniton recorded the call. The body holds the call's id. Runs start after it. You get no run data. |
   | `200` | a repeat of a known `webhook-id` with the same body: nothing starts |
   | `401` | the signature is not valid |
   | `404` | the endpoint is unknown, deleted or disabled, inbound webhooks are off, or your address is not in the allowed ranges |
   | `409` | a known `webhook-id` with another body |
   | `413` · `422` | the body is too large, too deep, has too many keys or is not `application/json` |
   | `429` | too many calls: wait for `Retry-After` |
   | `503` | the endpoint is paused: wait for `Retry-After` |

   A timestamp more than 5 minutes from Youniton's clock is refused. Keep your server's clock in sync.

## Security of the data

- Youniton verifies the signature over the raw bytes, in constant time, before it parses the body.
- Each value that arrives is untrusted data. It never becomes an instruction or a prompt of the flow.
- A run acts as the flow's owner. The caller gets no identity and no right.
- Youniton keeps a call's body for the tenant setting `automation.inbound.callRetentionDays`, and remembers each `webhook-id` for at least 7 days.

## The Admin operations

The Youniton app manages endpoints with these operations (session only):

- [`GET /automation/inbound-endpoints`](https://docs.youniton.com/api/reference/automation.md#listInboundEndpoints)The tenant's inbound endpoints
- [`POST /automation/inbound-endpoints`](https://docs.youniton.com/api/reference/automation.md#createInboundEndpoint)Register an endpoint; the answer holds the secret, once
- [`PATCH /automation/inbound-endpoints/{endpointId}`](https://docs.youniton.com/api/reference/automation.md#changeInboundEndpoint)Change the name, availability, address ranges or status
- [`POST /automation/inbound-endpoints/{endpointId}/rotate-secret`](https://docs.youniton.com/api/reference/automation.md#rotateInboundSecret)Issue a new secret, shown once; the previous one stays valid for 24 hours
- [`GET /automation/inbound-endpoints/{endpointId}/calls`](https://docs.youniton.com/api/reference/automation.md#listInboundCalls)The call log of an endpoint
- [`DELETE /automation/inbound-endpoints/{endpointId}`](https://docs.youniton.com/api/reference/automation.md#deleteInboundEndpoint)Delete an endpoint that no enabled or paused flow names
