# AIOZ Stream — Webhooks

SDK package: @aiozstream/nodejs-client

## Setup

```typescript
import StreamClient from '@aiozstream/nodejs-client'

const client = new StreamClient({
  publicKey: process.env.AIOZ_PUBLIC_KEY,
  secretKey: process.env.AIOZ_SECRET_KEY
})
```

Available webhook events:
- file_received: new video file successfully uploaded
- encoding_started: encoding process begins
- partial_finished: one quality level finished encoding
- encoding_finished: all encoding complete
- encoding_failed: encoding failed

---

## Create Webhook

POST https://api.aiozstream.network/api/webhooks

```typescript
const result = await client.webhook.create({
  name: 'my-webhook',
  url: 'https://your-server.com/webhook',
  fileReceived: true,
  encodingStarted: true,
  partialFinished: true,
  encodingFinished: true,
  encodingFailed: true
})
const webhookId = result.data.webhook.id
```

---

## Get Webhook

GET https://api.aiozstream.network/api/webhooks/{id}

```typescript
const result = await client.webhook.get(webhookId)
// result.data.webhook: { id, name, url, events..., last_triggered_at }
```

---

## List Webhooks

GET https://api.aiozstream.network/api/webhooks

```typescript
const result = await client.webhook.list({
  offset: 0,
  limit: 25,
  sortBy: 'created_at',
  orderBy: 'asc',
  search: 'name-filter'
})
// result.data.webhooks, result.data.total
```

---

## Update Webhook

PATCH https://api.aiozstream.network/api/webhooks/{id}

```typescript
await client.webhook.update(webhookId, {
  url: 'https://new-url.com/webhook',
  name: 'updated-name',
  encodingFinished: true,
  encodingFailed: true,
  fileReceived: false
})
```

---

## Delete Webhook

DELETE https://api.aiozstream.network/api/webhooks/{id}

```typescript
await client.webhook.delete(webhookId)
```

---

## Trigger Webhook (Test)

POST https://api.aiozstream.network/api/webhooks/check/{id}

```typescript
await client.webhook.check(webhookId)
```

---

## Handling Webhook Payloads (Express example)

```typescript
import express from 'express'

const app = express()
app.use(express.json())

app.post('/webhook', (req, res) => {
  const { event, data } = req.body

  switch (event) {
    case 'encoding_finished':
      console.log('Video ready:', data.videoId)
      // Update your DB, notify users, etc.
      break
    case 'encoding_failed':
      console.error('Encoding failed:', data.videoId)
      break
    case 'file_received':
      console.log('File received:', data.videoId)
      break
  }

  res.sendStatus(200)
})
```
