mcpField
D
deepgram-webhooks

deepgram-webhooks

github.com/hookdeck開発95200 回インストール今日更新
リポジトリ

deepgram-webhooks について

Deepgram の非同期文字起こしコールバックを処理するハンドラー構築向けのスキルです。コールバック認証、ペイロード項目、再試行動作、ローカルテストを説明します。

主な機能

  1. 01非同期文字起こしコールバックの処理
  2. 02コールバックURLのBasic Auth認証
  3. 03存在する場合のdg-token確認
  4. 04文字起こし結果ペイロードの読み取り
  5. 05Hookdeck CLIによるローカルテスト

活用シーン

  1. 01完了した文字起こし結果の受信
  2. 02Deepgramコールバックエンドポイントの保護
  3. 03ローカル開発中のwebhookペイロード確認

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/hookdeck/webhook-skills --skill deepgram-webhooks

インストール後、Claude Code で/deepgram-webhooksを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

examples/express/
.env.example
package.json
README.md
vitest.config.js
examples/express/src/
index.js
examples/express/test/
webhook.test.js
examples/fastapi/
.env.example
main.py
README.md
requirements.txt
test_webhook.py
examples/nextjs/
.env.example
package.json
README.md
vitest.config.ts
examples/nextjs/app/webhooks/deepgram/
route.ts
examples/nextjs/test/
webhook.test.ts
references/
overview.md
setup.md
verification.md

詳細

Deepgram Webhook

このスキルを使用する場面

  • 文字起こし結果を受信する Deepgram コールバックハンドラーを設定するとき
  • Deepgram から非同期で送信される文字起こし結果を処理するとき
  • Deepgram コールバックの Webhook 認証を実装するとき
  • 文字起こし完了イベントを処理するとき

基本的なコード

// Express.js example
const crypto = require('crypto');

function safeEqual(a, b) {
  const ab = Buffer.from(a || '', 'utf8');
  const bb = Buffer.from(b || '', 'utf8');
  return ab.length === bb.length && crypto.timingSafeEqual(ab, bb);
}

app.post('/webhooks/deepgram', express.raw({ type: 'application/json' }), (req, res) => {
  // 1. Primary check: Basic Auth credentials you embedded in the callback URL
  //    (https://user:[email protected]/webhooks/deepgram)
  const auth = req.headers['authorization'] || '';
  const decoded = auth.startsWith('Basic ')
    ? Buffer.from(auth.slice(6), 'base64').toString('utf8')
    : '';
  const sep = decoded.indexOf(':');
  if (
    sep === -1 ||
    !safeEqual(decoded.slice(0, sep), process.env.DEEPGRAM_CALLBACK_USERNAME) ||
    !safeEqual(decoded.slice(sep + 1), process.env.DEEPGRAM_CALLBACK_PASSWORD)
  ) {
    return res.status(401).send('Unauthorized');
  }

  // 2. Supplementary check: dg-token is NOT sent on every callback,
  //    so only compare it when it is present
  const dgToken = req.headers['dg-token'];
  if (dgToken && process.env.DEEPGRAM_API_KEY_ID && !safeEqual(dgToken, process.env.DEEPGRAM_API_KEY_ID)) {
    return res.status(403).send('Invalid dg-token');
  }

  // The callback body is the normal /v1/listen response: { metadata, results }
  const payload = JSON.parse(req.body.toString());
  const requestId = payload.metadata?.request_id;
  const transcript = payload.results?.channels?.[0]?.alternatives?.[0]?.transcript;
  console.log('Received transcription:', requestId, transcript);

  // Return success to prevent retries
  res.status(200).send('OK');
});
認証方法

Deepgram では、コールバックを認証する方法として次の 2 つが記載されています。

  1. Basic Auth(主要な方法):コールバック URL に認証情報を埋め込みます。Deepgram は Authorization: Basic ヘッダーを送信します
  2. dg-token ヘッダー(補助的な方法):このヘッダーがある場合、リクエストを送信した API キーの識別子が含まれます。Deepgram のドキュメントには「すべてのコールバックリクエストに含まれるとは限らない」と記載されているため、これだけに依存しないでください
// Basic Auth in callback URL (percent-encode special characters in the credentials)
// https://username:[email protected]/webhooks/deepgram

// dg-token: check only when present
const dgToken = req.headers['dg-token'];
if (dgToken && dgToken !== process.env.DEEPGRAM_API_KEY_ID) {
  return res.status(403).send('Invalid dg-token');
}
コールバックを指定してリクエストを送信する
curl \
  --request POST \
  --header 'Authorization: Token YOUR_DEEPGRAM_API_KEY' \
  --header 'Content-Type: audio/wav' \
  --data-binary @audio.wav \
  --url 'https://api.deepgram.com/v1/listen?callback=https://username:[email protected]/webhooks/deepgram'

一般的なイベントタイプ

Deepgram のコールバックにはイベントタイプのフィールドがありません。本文は、同期的な /v1/listen リクエストが返すものと同じ JSON で、metadata オブジェクトと results オブジェクトが含まれます。results の構造は、リクエストで有効にした機能によって異なります。

フィールド説明常に存在する
metadata.request_id文字起こしリクエストの一意の識別子(送信時に返される request_id と一致します)はい
metadata.created文字起こしが作成された時刻はい
metadata.duration音声の長さ(秒)はい
metadata.channels音声チャンネル数はい
metadata.extraextra=KEY:VALUE で渡したキーと値のペアextra を送信した場合のみ
results.channels[].alternatives文字起こしの候補はい
results.channels[].alternatives[].transcript文字起こしされたテキストはい
results.channels[].alternatives[].confidence信頼度スコア(0~1)はい

環境変数

# Your Deepgram API Key (for making requests)
DEEPGRAM_API_KEY=your_api_key_here

# Basic Auth credentials you embed in the callback URL (primary check)
DEEPGRAM_CALLBACK_USERNAME=your_callback_username
DEEPGRAM_CALLBACK_PASSWORD=your_callback_password

# Optional: API Key Identifier, used to check the dg-token header when present
# Note: This is NOT your API Key secret - it's a unique identifier shown
# in the Deepgram console that identifies which API key was used for a request
DEEPGRAM_API_KEY_ID=your_api_key_id_here

# Your webhook endpoint URL
WEBHOOK_URL=https://your-domain.com/webhooks/deepgram

ローカル開発

ローカルで Webhook をテストするには、Hookdeck CLI をインストールしてください。

# Create a local tunnel (no account required)
npx hookdeck-cli listen 3000 deepgram --path /webhooks/deepgram

# Use the provided URL as your callback URL when making Deepgram requests

次の機能を利用できます。

  • テスト用のローカルトンネル URL
  • Webhook ペイロードを確認する Web UI
  • リクエスト履歴とデバッグツール

重要な注意事項

再試行の動作
  • Deepgram は失敗したコールバック(200~299 以外のステータス)を最大 10 回再試行します
  • 再試行の間隔は 30 秒です
  • 正常に処理された Webhook には、必ず 200~299 のステータスを返してください
ポートの制限
  • コールバックに使用できるポートは 80、443、8080、8443 のみです
  • Webhook エンドポイントがこれらのいずれかのポートを使用していることを確認してください
署名検証なし
  • Deepgram はコールバックに署名を付与しません(HMAC もタイムスタンプもありません)
  • コールバック URL に埋め込んだ Basic Auth の認証情報を使って認証してください。dg-token ヘッダーはすべてのコールバックに送信されるわけではないため、補助的な確認にとどめてください
  • Webhook エンドポイントには必ず HTTPS を使用してください

リソース

  • overview.md - Deepgram Webhook の概要と文字起こしイベント
  • setup.md - Deepgram API リクエストでのコールバック設定
  • verification.md - 認証方法とセキュリティ上の考慮事項
  • examples/ - Express、Next.js、FastAPI の完全な実装例

推奨:webhook-handler-patterns

本番環境のハンドラー向けに、このスキルと合わせてパターンスキルをインストールしてください。主なリファレンス(このスキルのみをインストールしている場合もリンクは機能します)。

関連スキル

コメント