Create a media generation task
Creation of the task
Section titled “Creation of the task”https://gptunnel.ru/api/v2/media/tasksUse this endpoint to create a task for any media model. The response contains a task object with its ID and status. Save id to poll the task and match it to the webhook notification. The webhook sends task data when the task finishes.
Request body
| Parameter | Type | Description |
|---|---|---|
model* | string | Model Identifier from GET /media/models |
prompt | string | Text for generation. The prompt field in the model card specifies whether the field is required (required, optional, none). If max_length or max_bytes are specified, check the text length in UTF-8 characters or bytes respectively. |
params | object | Generation parameters according to the schema of the selected model (resolution, duration, etc.) |
inputs | object | Input files grouped by role in an object such as {role: [url, ...]}. Use public URLs or inline data: URLs. Each inline file can be up to 20 MB. |
webhook_url | string | URL to which we will send the task object after completion. See Webhooks section |
idempotency_key | string | Idempotency key: a repeat request with the same key returns an already created task, not a new one |
wait | boolean | If true — wait for completion up to 30 seconds, then return the current task status. Default: false |
Parameter wait
Section titled “Parameter wait”By default (wait: false), task creation returns immediately with status queued or running. Get the result by polling GET /media/tasks/:id or receiving a webhook.
With wait: true, the connection stays open until the task reaches done or failed, or for approximately 30 seconds. If the task is still queued or running at the end of that window, the response contains its current state. Poll GET /media/tasks/:id or wait for the webhook to get the final result.
Idempotency Key
Section titled “Idempotency Key”Set idempotency_key to avoid creating a duplicate task or paying twice when retrying after a network timeout. Retrying with the same key and request body returns the original task.
If you repeat the request with the same idempotency_key, but with a modified request body, an error will be returned 409 (code: 23).
Example request
Section titled “Example request”Set the environment variable GPTUNNEL_API_KEY. The request creates a paid task. To know the cost in advance, send the same model, prompt and params in calculation of price. For a new generation, set a new idempotency_key. If you repeat the same operation, keep the old key and body.
import jsonimport osfrom urllib.request import Request, urlopen
body = { 'model': 'kling-v3', 'prompt': 'a red fox running through snow, cinematic', 'params': {'resolution': '1080p', 'duration': 5, 'aspect_ratio': '16:9'}, 'idempotency_key': 'fox-snow-001',}request = Request( 'https://gptunnel.ru/api/v2/media/tasks', method='POST', headers={'Authorization': os.environ['GPTUNNEL_API_KEY'], 'Content-Type': 'application/json'}, data=json.dumps(body).encode('utf-8'),)with urlopen(request, timeout=30) as response: task = json.load(response)print(task['id'], task['status'])curl --request POST \ --url 'https://gptunnel.ru/api/v2/media/tasks' \ --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \ --header 'Content-Type: application/json' \ --data '{ "model": "kling-v3", "prompt": "a red fox running through snow, cinematic", "params": { "resolution": "1080p", "duration": 5, "aspect_ratio": "16:9" }, "idempotency_key": "fox-snow-001" }'const key = process.env.GPTUNNEL_API_KEYif (!key) throw new Error('Set GPTUNNEL_API_KEY')
const response = await fetch('https://gptunnel.ru/api/v2/media/tasks', { method: 'POST', headers: { Authorization: key, 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'kling-v3', prompt: 'a red fox running through snow, cinematic', params: { resolution: '1080p', duration: 5, aspect_ratio: '16:9' }, idempotency_key: 'fox-snow-001', }),})if (!response.ok) throw new Error(await response.text())const task = await response.json()console.log(task.id, task.status)Save it id from the response for task polling. To get notification, add to the field webhook_url your HTTPS webhook address.
File uploads
Section titled “File uploads”Pass files in inputs under the roles listed in the model schema (inputs[].role). Each role takes an array of URLs.
{ "model": "kling-v3", "prompt": "animate this photo", "inputs": { "first_frame": ["https://example.com/photo.jpg"] }}Replace https://example.com/photo.jpg with a direct public URL to your image.
File can be delivered as data:-URL. Below is a body fragment; replace the reduced line with full file contents in Base64:
{ "inputs": { "first_frame": ["data:image/png;base64,iVBORw0KGgo..."] }}The limits are 20 MB per file and 50 MB for the entire request body. An invalid or unreadable file returns code: 25 immediately. The service creates no task and makes no charge.
Example response
Section titled “Example response”{ "code": 0, "id": "6a61e021a6e77553a989c41d", "model": "kling-v3", "status": "running", "prompt": "a red fox running through snow, cinematic", "params": { "resolution": "1080p", "duration": 5, "aspect_ratio": "16:9" }, "price": 210, "result": [], "created_at": 1784799265}If you send webhook_url, the response also includes a webhook object with the destination URL and delivery status. See task object fields.
If the model has been removed from service
Section titled “If the model has been removed from service”If the provider retires the requested model and the catalog lists a replacement, the API creates the task on that replacement. The request works with the same code and is billed at the requested model’s rate.
Check the response headers to see which model ran the task.
Response headers
| Parameter | Type | Description |
|---|---|---|
Deprecation | string | Always true. Indicates that the requested model has been retired. |
Sunset | string | Retirement date of the requested model, in HTTP-date format. |
x-model-requested | string | ID of the model you requested. Its rate is used for billing. |
x-model-served | string | ID of the model that runs the task. |
The task object’s model remains the ID you requested. The x-model-served response header identifies the replacement used. The creation response includes a warnings entry with code: "DEPRECATED_MODEL". If the model has no replacement, task creation returns 410 with code: 21. See Deprecated models.