0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Omni Flash API でテキストプロンプトから動画を生成する(Node.js 実装ガイド)

0
Posted at

ブラウザ上で動く AI 動画生成サービス Omni Flash には開発者向けの REST API が用意されています。本記事では、テキストプロンプトから動画を生成する一連のフロー(タスク作成 → ポーリング → 結果取得)を Node.js で実装する方法を解説します。

API キーの取得

API キーは公式サイトのアカウントページから発行できます。Omni Flash にログインし、アカウント画面(omniflash.net/account)で API キーを生成してください。発行されるキーは sk- から始まる文字列です。

すべてのリクエストで Bearer トークンとして送信します。

Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

エンドポイントの基本

  • ベース URL: https://omniflash.net/api/v1
  • タスク作成: POST /api/v1/tasks/create
  • タスク照会: GET /api/v1/tasks/{task_id}

動画/画像生成は非同期処理です。まずタスクを作成して task_id を受け取り、そのあと照会エンドポイントを定期的に叩いて完了を待つ、という流れになります。

利用できるモデル

model_id 用途 出力
omni-flash テキスト/画像 → 動画 video_url
omni-pro テキスト/画像 → 動画 video_url
seedance-2 テキスト/画像 → 動画 video_url
gpt-image-2 テキスト/画像 → 画像 image_url
nano-banana-2 テキスト/画像 → 画像 image_url

今回は動画生成なので omni-flash を使います。

タスクを作成する

POST /api/v1/tasks/create に以下の JSON を送ります。

{
  "model_id": "omni-flash",
  "prompt": "a serene zen garden at sunrise, gentle camera pan",
  "aspect_ratio": "16:9"
}

aspect_ratio には横向きの 16:9 と縦向きの 9:16 を指定できます。画像から動画を生成したい場合は image_urls に入力画像の URL を配列で渡します。

レスポンスはこの形です。

{
  "code": 200,
  "msg": "submission successful",
  "data": {
    "task_id": "abcdef123456",
    "request_id": "kie_xxxxxxxxxxxx",
    "credits": 15
  }
}

クレジットは送信時点で差し引かれます。後述の通りタスクが失敗した場合は自動で返却されます。

ポーリングで結果を取得する

GET /api/v1/tasks/{task_id} のレスポンスにある task_status を見て完了を判定します。

task_status 状態
1 キュー待ち
2 実行中
3 成功
4 失敗

3(成功)になったら data.video_url から動画を取得できます。

Node.js 実装サンプル

Node.js 18 以降であれば標準の fetch が使えます。

const BASE_URL = "https://omniflash.net/api/v1";
const API_KEY = process.env.OMNIFLASH_API_KEY;

const headers = {
  "Authorization": `Bearer ${API_KEY}`,
  "Content-Type": "application/json",
};

async function createTask(prompt) {
  const res = await fetch(`${BASE_URL}/tasks/create`, {
    method: "POST",
    headers,
    body: JSON.stringify({
      model_id: "omni-flash",
      prompt,
      aspect_ratio: "16:9",
    }),
  });
  const json = await res.json();
  if (json.code !== 200) {
    throw new Error(`create failed: ${json.msg}`);
  }
  return json.data.task_id;
}

async function waitForResult(taskId, { intervalMs = 5000, timeoutMs = 600000 } = {}) {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    const res = await fetch(`${BASE_URL}/tasks/${taskId}`, { headers });
    const json = await res.json();
    const { task_status, video_url } = json.data;

    if (task_status === 3) return video_url;
    if (task_status === 4) throw new Error("task failed (credits refunded)");

    await new Promise((r) => setTimeout(r, intervalMs));
  }
  throw new Error("polling timed out");
}

(async () => {
  const taskId = await createTask("a serene zen garden at sunrise, gentle camera pan");
  console.log("task created:", taskId);

  const videoUrl = await waitForResult(taskId);
  console.log("video ready:", videoUrl);
})();

エラーハンドリングの勘所

  • code: 200 … 成功
  • code: 0 … ビジネスロジック上の失敗。msg フィールドに原因が入るので必ずログに残す
  • code: 401 … API キーが無効、または未指定

task_status4(失敗)になった場合、消費したクレジットは自動的に返却されます。アプリ側で再キューイングする際はこの挙動を踏まえて、二重課金を心配せず素直にリトライして問題ありません。

まとめ

Omni Flash の API は「タスク作成 → ポーリング → URL 取得」というシンプルな非同期パターンで動画生成を組み込めます。インストール不要でブラウザから使える本体に加えて、こうした API があることでバッチ処理や自社サービスへの組み込みも容易です。まずは Omni Flash にログインして API キーを発行し、上のサンプルを動かしてみてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?