# AIOZ Stream — Live Streaming

SDK package: @aiozstream/nodejs-client
Note: Live Streaming API is not yet in the SDK — use fetch with API key headers.

## Setup

```typescript
const HEADERS = {
  'Content-Type': 'application/json',
  'stream-public-key': process.env.AIOZ_PUBLIC_KEY,
  'stream-secret-key': process.env.AIOZ_SECRET_KEY
}
const BASE = 'https://api.aiozstream.network/api'
```

A live stream key must exist before creating a streaming session. Create keys in the dashboard or via the API.

---

## Create Live Streaming Session

POST https://api.aiozstream.network/api/live_streams/{keyId}/streamings

```typescript
async function createLiveStream(keyId: string, title: string, save = false) {
  const res = await fetch(`${BASE}/live_streams/${keyId}/streamings`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({
      title,
      save,
      qualities: [
        { resolution: '360p', type: 'hls', container_type: 'mpegts' },
        { resolution: '720p', type: 'hls', container_type: 'mpegts' }
      ]
    })
  })
  return res.json()
}

const stream = await createLiveStream('key_id', 'My Live Stream', true)
// stream.data.id         — streaming session ID
// stream.data.status     — 'created'
// stream.data.assets.hlsUrl
// stream.data.assets.playerUrl
// stream.data.assets.iframe
// stream.data.currentView, totalView
```

Quality resolutions: '240p' | '360p' | '480p' | '720p' | '1080p'
container_type for hls: 'mpegts' | 'mp4'
container_type for dash: 'fmp4'

---

## Get Live Stream Statistics

GET https://api.aiozstream.network/api/live_streams/statistic/{streamMediaId}

Statistics update every 5–30 seconds during an active stream.

```typescript
async function getLiveStreamStats(streamMediaId: string) {
  const res = await fetch(
    `${BASE}/live_streams/statistic/${streamMediaId}`,
    { headers: HEADERS }
  )
  return res.json()
}

const stats = await getLiveStreamStats('stream_media_id')
// stats.data.fps_in          — studio upload frame rate
// stats.data.fps_out         — delivery frame rate to viewers
// stats.data.bitrate_in      — studio upload bitrate (Kbps)
// stats.data.bitrate_out     — delivery bitrate to viewers (Kbps)
// stats.data.data_transferred — total MB sent from studio
```

---

## List Saved Live Stream Media

POST https://api.aiozstream.network/api/live_streams/{keyId}/media

Lists media saved from past live streams (requires save: true during stream).

```typescript
async function listSavedMedia(keyId: string, options = {}) {
  const res = await fetch(`${BASE}/live_streams/${keyId}/media`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({
      offset: 0,
      limit: 25,
      sort_by: 'created_at',
      order_by: 'desc',
      ...options
    })
  })
  return res.json()
}

const saved = await listSavedMedia('key_id')
// saved.data.videos: [{id, title, duration, assets, status, video: {...}}]
// saved.data.total
```

---

## Add/Update Multicast RTMP URLs

POST https://api.aiozstream.network/api/live_streams/multicast/{streamKey}

Simultaneously forward a live stream to YouTube, Twitch, Facebook, etc.

```typescript
async function setMulticastUrls(streamKey: string, urls: string[]) {
  const res = await fetch(`${BASE}/live_streams/multicast/${streamKey}`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({ multicast_urls: urls })
  })
  return res.json()
}

await setMulticastUrls('your_stream_key', [
  'rtmp://a.rtmp.youtube.com/live2/your-youtube-key',
  'rtmp://live.twitch.tv/live/your-twitch-key'
])
```
