# Webhook

The module allows you to create subscriptions for events that occur in the Crowdin project or organization. It's not necessary to subscribe to events every time after creating a new project, events will come from all resources allowed for the application.

## Sample

```js ins={14-21} title="index.js"
    import crowdinModule from '@crowdin/app-project-module';

    const app = crowdinModule.express();

    const configuration = {
      baseUrl: 'https://123.ngrok.io',
      clientId: 'clientId',
      clientSecret: 'clientSecret',
      name: 'Sample App',
      identifier: 'sample-app',
      description: 'Sample App description',
      dbFolder: import.meta.dirname,
      imagePath: import.meta.dirname + '/logo.png',
      webhooks: [
        {
          events: ['file.added', 'file.updated', 'file.deleted', 'file.reverted'],
          callback({client, events, webhookContext}) {
            console.log('File events:', events);
          }
        }
      ]
    }
    const crowdinApp = crowdinModule.addCrowdinEndpoints(app, configuration);

    app.listen(3000, () =>  console.log('Crowdin app started'));
    ```
  ```ts ins={15-22} title="index.ts"
    import crowdinModule from '@crowdin/app-project-module';
    import type { ClientConfig } from '@crowdin/app-project-module';

    const app = crowdinModule.express();

    const configuration: ClientConfig = {
      baseUrl: 'https://123.ngrok.io',
      clientId: 'clientId',
      clientSecret: 'clientSecret',
      name: 'Sample App',
      identifier: 'sample-app',
      description: 'Sample App description',
      dbFolder: import.meta.dirname,
      imagePath: import.meta.dirname + '/logo.png',
      webhooks: [
        {
          events: ['file.added', 'file.updated', 'file.deleted', 'file.reverted'],
          async callback({client, events, webhookContext}) {
            console.log('File events:', events);
          }
        }
      ]
    }
    const crowdinApp = crowdinModule.addCrowdinEndpoints(app, configuration);

    app.listen(3000, () =>  console.log('Crowdin app started'));
    ```
  To register or update webhooks programmatically (for example during app setup), use `createOrUpdateWebhook` and related helpers from [App Functions](/app-project-module/reference/app-functions/#crowdin-functions).

## Configuration

| Parameter<div class="w-32"/> | Description                                                                                                           |
|------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `events`                     | List of events to subscribe the application. [See available events](https://support.crowdin.com/developer/webhooks/). |
| `callback`                   | This function is called when the app receives events.                                                                 |
| `deferResponse`              | Optional. If `true`, the HTTP response is sent after webhook processing completes. Required for Cloudflare Workers. Default: `false`. |

### `callback` Function

#### Parameters

- `client` - Crowdin API client.
- `events` - Array of webhook event objects.
- `webhookContext` - Object containing webhook context information:
  - `domain` - Crowdin domain
  - `organizationId` - Organization ID
  - `userId` - User ID (user who installed the application)
  - `agentId` - Agent ID (if `authenticationType` is `crowdin_agent`)

## Cloudflare Workers

For Cloudflare Workers, set `deferResponse: true` to prevent execution context termination before webhook processing completes.

```js title="worker.js"
webhooks: [{
  events: ['file.added', 'file.updated'],
  deferResponse: true, // Required for Workers
  callback: async ({ client, events }) => {
    // Process webhook events
    for (const event of events) {
      await processEvent(event, client);
    }
  }
}]
```

**Why needed:** Workers terminate execution immediately after HTTP response is sent. `deferResponse: true` sends the response after callback completion, ensuring proper execution.