Skip to content

Async Media Tasks

Image generation, image editing, and video generation can take longer than a normal HTTP request. Image requests can use either the standard endpoints or /v1/async/* endpoints. Video generation is async-only and must be submitted through /v1/async/videos/generations.

Endpoints

WorkloadAsync endpoint
Text-to-imagePOST /v1/async/images/generations
Image editingPOST /v1/async/images/edits
Text-to-video or image-to-videoPOST /v1/async/videos/generations

Async image request bodies stay the same as the matching image endpoints. Video requests use the async video endpoint directly. Do not add async=true query parameters or "async": true body fields.

Submit a task

bash
curl https://api.pixapi.ai/v1/async/images/generations \
  -H "Authorization: Bearer $PIXAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "A cinematic product photo of a ceramic coffee cup",
    "size": "2048x2048"
  }'
py
import os
import requests

response = requests.post(
    "https://api.pixapi.ai/v1/async/images/generations",
    headers={
        "Authorization": f"Bearer {os.environ['PIXAPI_KEY']}",
        "Content-Type": "application/json",
    },
    json={
        "model": "gemini-3.1-flash-image-preview",
        "prompt": "A cinematic product photo of a ceramic coffee cup",
        "size": "2048x2048",
    },
)

response.raise_for_status()
task = response.json()
print(task["id"])
js
const response = await fetch('https://api.pixapi.ai/v1/async/images/generations', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.PIXAPI_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'gemini-3.1-flash-image-preview',
    prompt: 'A cinematic product photo of a ceramic coffee cup',
    size: '2048x2048',
  }),
});

if (!response.ok) {
  throw new Error(await response.text());
}

const task = await response.json();
console.log(task.id);
go
package main

import (
  "bytes"
  "fmt"
  "io"
  "net/http"
  "os"
)

func main() {
  body := []byte(`{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "A cinematic product photo of a ceramic coffee cup",
    "size": "2048x2048"
  }`)

  req, err := http.NewRequest(
    "POST",
    "https://api.pixapi.ai/v1/async/images/generations",
    bytes.NewBuffer(body),
  )
  if err != nil {
    panic(err)
  }
  req.Header.Set("Authorization", "Bearer "+os.Getenv("PIXAPI_KEY"))
  req.Header.Set("Content-Type", "application/json")

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  result, _ := io.ReadAll(resp.Body)
  fmt.Println(string(result))
}
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

class PixapiAsyncExample {
  public static void main(String[] args) throws Exception {
    String body = """
      {
        "model": "gemini-3.1-flash-image-preview",
        "prompt": "A cinematic product photo of a ceramic coffee cup",
        "size": "2048x2048"
      }
      """;

    HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://api.pixapi.ai/v1/async/images/generations"))
      .header("Authorization", "Bearer " + System.getenv("PIXAPI_KEY"))
      .header("Content-Type", "application/json")
      .POST(HttpRequest.BodyPublishers.ofString(body))
      .build();

    HttpResponse<String> response = HttpClient.newHttpClient()
      .send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response.body());
  }
}
php
<?php
$payload = json_encode([
  "model" => "gemini-3.1-flash-image-preview",
  "prompt" => "A cinematic product photo of a ceramic coffee cup",
  "size" => "2048x2048",
]);

$ch = curl_init("https://api.pixapi.ai/v1/async/images/generations");
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("PIXAPI_KEY"),
    "Content-Type: application/json",
  ],
  CURLOPT_POSTFIELDS => $payload,
]);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

Pixapi returns immediately after the task is submitted:

json
{
  "status": "submitted",
  "id": "task_01KPQ7J7DWB7QZ3WCEK3YVPBRA",
  "progress": 0,
  "created_at": 1703884800,
  "model": "gemini-3.1-flash-image-preview"
}

Query a task

Use the task id returned by the async endpoint:

bash
curl --request GET \
  --url 'https://api.pixapi.ai/v1/tasks/task_01KPQ7J7DWB7QZ3WCEK3YVPBRA' \
  --header 'Authorization: Bearer <token>'
py
import os
import requests

task_id = "task_01KPQ7J7DWB7QZ3WCEK3YVPBRA"

response = requests.get(
    f"https://api.pixapi.ai/v1/tasks/{task_id}",
    headers={"Authorization": f"Bearer {os.environ['PIXAPI_KEY']}"},
)

response.raise_for_status()
task = response.json()
print(task["status"])
print(task["progress"])
js
const taskId = 'task_01KPQ7J7DWB7QZ3WCEK3YVPBRA';

const response = await fetch(`https://api.pixapi.ai/v1/tasks/${taskId}`, {
  headers: {
    Authorization: `Bearer ${process.env.PIXAPI_KEY}`,
  },
});

if (!response.ok) {
  throw new Error(await response.text());
}

const task = await response.json();
console.log(task.status, task.progress);
go
package main

import (
  "fmt"
  "io"
  "net/http"
  "os"
)

func main() {
  taskID := "task_01KPQ7J7DWB7QZ3WCEK3YVPBRA"

  req, err := http.NewRequest(
    "GET",
    "https://api.pixapi.ai/v1/tasks/"+taskID,
    nil,
  )
  if err != nil {
    panic(err)
  }
  req.Header.Set("Authorization", "Bearer "+os.Getenv("PIXAPI_KEY"))

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  result, _ := io.ReadAll(resp.Body)
  fmt.Println(string(result))
}
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

class PixapiTaskStatus {
  public static void main(String[] args) throws Exception {
    String taskId = "task_01KPQ7J7DWB7QZ3WCEK3YVPBRA";

    HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://api.pixapi.ai/v1/tasks/" + taskId))
      .header("Authorization", "Bearer " + System.getenv("PIXAPI_KEY"))
      .GET()
      .build();

    HttpResponse<String> response = HttpClient.newHttpClient()
      .send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response.body());
  }
}
php
<?php
$taskId = "task_01KPQ7J7DWB7QZ3WCEK3YVPBRA";

$ch = curl_init("https://api.pixapi.ai/v1/tasks/" . $taskId);
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("PIXAPI_KEY"),
  ],
]);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

Completed tasks return the generated media under result.data[]:

json
{
  "id": "xxx",
  "status": "completed",
  "credits_cost": 1.5,
  "model": "sora-2",
  "progress": 100,
  "result": {
    "type": "video",
    "data": [
      {
        "url": "https://xxx.xxx"
      }
    ]
  },
  "created": 1763088289,
  "completed": 1763088308
}

Status values

StatusMeaning
submittedPixapi accepted the task and queued it for processing.
processingThe upstream model is generating media.
completedThe task completed and result is available.
failedThe task failed. Read the error object.

Error format

json
{
  "error": {
    "code": 400,
    "message": "xxx",
    "type": "invalid_request_error"
  }
}

Integration pattern

  1. Submit long-running requests through /v1/async/images/generations, /v1/async/images/edits, or /v1/async/videos/generations.
  2. Store the returned id in your database.
  3. Poll GET /v1/tasks/{id} from your backend with exponential backoff.
  4. Show status and progress in your product UI.
  5. Save the final result.data[] URLs when the task reaches completed.

Do not poll from browser-only code with your Pixapi key. Route polling through your backend just like generation requests.