언어별 예제
재생 주소 발급과 방송 상태 조회를 서버에서 부르는 최소 예제입니다. 추가 설치 없이 돌아갑니다.
Node.js (18 이상)
const BASE = "https://console.ondakr.com/api/v1";
const KEY = process.env.ONDA_API_KEY;
async function api(method, path, body) {
const res = await fetch(`${BASE}/${path}`, {
method,
headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
body: body ? JSON.stringify(body) : undefined,
});
const json = await res.json();
if (!res.ok) throw new Error(`${res.status} ${json.error?.code}: ${json.error?.message}`);
return json;
}
// 시청자가 재생 화면을 열 때
export const playbackFor = (channelId, viewer) =>
api("POST", `channels/${channelId}/playback`, {
ip: viewer.ip,
viewerId: viewer.id,
authenticated: viewer.loggedIn,
sessionId: viewer.sessionId, // 갱신할 때만
});
// 방송 상태: "live" | "idle" | "blocked"
export const channelStatus = async (channelId) => (await api("GET", `channels/${channelId}`)).status;Python (3.9 이상)
import json, os, urllib.request, urllib.error
BASE = "https://console.ondakr.com/api/v1"
KEY = os.environ["ONDA_API_KEY"]
def api(method, path, body=None):
req = urllib.request.Request(
f"{BASE}/{path}", method=method,
data=json.dumps(body).encode() if body is not None else None,
headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(req) as res:
return json.load(res)
except urllib.error.HTTPError as e:
err = json.load(e).get("error", {})
raise RuntimeError(f"{e.code} {err.get('code')}: {err.get('message')}")
def playback_for(channel_id, ip, viewer_id=None, logged_in=False, session_id=None):
body = {"ip": ip, "authenticated": logged_in}
if viewer_id: body["viewerId"] = viewer_id
if session_id: body["sessionId"] = session_id
return api("POST", f"channels/{channel_id}/playback", body)
def channel_status(channel_id):
return api("GET", f"channels/{channel_id}")["status"]PHP (8 이상, curl 확장)
<?php
const BASE = 'https://console.ondakr.com/api/v1';
function api(string $method, string $path, ?array $body = null): array {
$ch = curl_init(BASE . '/' . $path);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('ONDA_API_KEY'), 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => $body === null ? null : json_encode($body),
]);
$json = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status >= 400) throw new RuntimeException("$status {$json['error']['code']}: {$json['error']['message']}");
return $json;
}
function playback_for(string $channelId, string $ip, ?string $viewerId = null, bool $loggedIn = false, ?string $sessionId = null): array {
return api('POST', "channels/$channelId/playback", array_filter(
['ip' => $ip, 'viewerId' => $viewerId, 'authenticated' => $loggedIn, 'sessionId' => $sessionId],
fn ($v) => $v !== null,
));
}웹훅 서명 확인 코드는 웹훅 문서에 있습니다.