> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eazecustoms.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Examples

> Configure an endpoint in the portal, verify the delivery, and read the current resource.

## What you can do

This is one complete path: configure the endpoint, check the signature, ignore a duplicate, and GET the Declaration.

> [!WARNING]
>
> **Security requirement: Always verify the `Eaze-Signature` header using your webhook signing secret before processing events. Requests without a valid signature must be rejected.**

## Configure the endpoint

1. Sign in to the Eaze Customs Portal.
2. Open **Global Modules**, then **Brokers**. Open the broker and select **System Config**.
3. On the **Webhooks** card, select the add button. Its tooltip is **Create Webhook**, and the dialog title is **Create Webhook**.
4. Enter a name and `https://broker.example.com/eaze/webhooks`.
5. In **Event**, select **import.declaration.released**. You can add the other events from [Webhook Events](https://docs.eazecustoms.com/webhooks/events.md).
6. Select **Save**.
7. Copy the signing secret from the **Signing secret** dialog and store it as `EAZE_WEBHOOK_SECRET`. It is not shown again.

The full screen reference is [Set Up Webhooks](https://docs.eazecustoms.com/webhooks/setup.md).

## Receive, verify, and read

This TypeScript receiver uses the raw body. It rejects a bad signature with HTTP 401, remembers the event ID, and then reads the Declaration. The in-memory set is only a sketch. Store event IDs in durable storage so a restart does not apply the same event twice.

```typescript theme={null}
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';

const secret = process.env.EAZE_WEBHOOK_SECRET ?? '';
const credential = process.env.EAZE_CUSTOMS_API_CREDENTIAL ?? '';
const baseUrl = 'https://staging.api.eazecustoms.com';
const seen = new Set<string>();

type WebhookEvent = {
  id: string;
  type: string;
  clientRef: string;
  data: { resource: { id: string } };
};

function header(headers: IncomingMessage['headers'], name: string): string | undefined {
  const value = headers[name];
  return Array.isArray(value) ? value[0] : value;
}

function verifyWebhook(rawBody: string, headers: IncomingMessage['headers']): WebhookEvent | null {
  const timestamp = header(headers, 'eaze-timestamp');
  const eventId = header(headers, 'eaze-event-id');
  const signature = header(headers, 'eaze-signature');
  if (!secret || !timestamp || !eventId || !signature) return null;
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return null;

  const expected = `sha256=${createHmac('sha256', secret)
    .update(`${timestamp}.${eventId}.${rawBody}`)
    .digest('hex')}`;
  const left = Buffer.from(expected);
  const right = Buffer.from(signature);
  if (left.length !== right.length || !timingSafeEqual(left, right)) return null;

  let event: WebhookEvent;
  try {
    event = JSON.parse(rawBody) as WebhookEvent;
  } catch {
    return null;
  }
  return event.id === eventId ? event : null;
}

function readBody(request: IncomingMessage): Promise<string> {
  return new Promise((resolve, reject) => {
    const chunks: Buffer[] = [];
    request.on('data', (chunk: Buffer) => chunks.push(chunk));
    request.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
    request.on('error', reject);
  });
}

const paths: Record<string, (event: WebhookEvent) => string> = {
  'import.declaration.released': (event) => `/public/v1/declarations/${event.data.resource.id}`,
  'import.declaration.completed': (event) => `/public/v1/declarations/${event.data.resource.id}`,
  'ncts.departure.released': (event) => `/public/v1/ncts/transit-declarations/${event.data.resource.id}`,
  'ncts.arrival.released': (event) => `/public/v1/ncts/transit-declarations/${event.data.resource.id}`,
  'rto.completed': (event) => `/public/v1/shipments/${event.data.resource.id}`,
};

async function readResource(event: WebhookEvent): Promise<void> {
  const pathFor = paths[event.type];
  if (!pathFor) return;
  const response = await fetch(`${baseUrl}${pathFor(event)}`, {
    headers: {
      Authorization: `Bearer ${credential}`,
      'X-Eaze-Client-Ref': event.clientRef,
    },
  });
  if (!response.ok) {
    throw new Error(`GET ${pathFor(event)} failed with HTTP ${response.status}`);
  }
}

const server = createServer(async (request, response: ServerResponse) => {
  if (request.method !== 'POST' || request.url !== '/eaze/webhooks') {
    response.writeHead(404).end();
    return;
  }

  const event = verifyWebhook(await readBody(request), request.headers);
  if (!event) {
    response.writeHead(401).end();
    return;
  }
  if (seen.has(event.id)) {
    response.writeHead(204).end();
    return;
  }

  seen.add(event.id);
  response.writeHead(204).end();

  try {
    await readResource(event);
  } catch (error) {
    console.error('Verified event was stored. Read the resource again.', event.id, error);
  }
});

server.listen(8080);
```

Send `event.clientRef` as `X-Eaze-Client-Ref` on the follow-up GET. **Staging — For Integration Testing:** `https://staging.api.eazecustoms.com`. Do not append `/public/v1` to that host.

## Handle repeated deliveries and errors

| Situation | What to do |
| - | - |
| The signature is missing or invalid | Return HTTP 401. Do not read the resource. This response is not retried. |
| The event ID is already stored | Return HTTP 204. Do not apply the event again. |
| The GET fails after you stored the ID | Call the same GET again later. A 204 tells Eaze Customs the notification was accepted, so that delivery is not retried for a GET failure. |
| You cannot store the event ID | Return HTTP 500 after a valid signature. Eaze Customs retries temporary failures. Do not return 500 for a bad signature. |
| The same event arrives again from **Retry** | The event ID is unchanged. Acknowledge it when you have already processed it. |

## What each event should trigger

| Event | Follow-up GET |
| - | - |
| `import.declaration.released` | Get Declaration, then list documents if you need a PDF. |
| `import.declaration.completed` | Get Declaration. |
| `ncts.departure.released` | Get Transit Declaration, then list transit documents. |
| `ncts.arrival.released` | Get Transit Declaration. |
| `rto.completed` | Get Shipment and, if you need detail, list movements. |

A 204 from your endpoint means you accepted the notification. It does not update the business resource. The GET response is the record to trust.

Signature details are on [Verify Signatures](https://docs.eazecustoms.com/webhooks/verify-signatures.md). Delivery history and **Retry** are on [Deliveries & Retries](https://docs.eazecustoms.com/webhooks/retries.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.