Kling API: Generate Video, Poll Tasks, and Check Billing
Generate a short Kling video through a documented BetterToken workflow or the official Kling 4.0 API. Keep provider-specific request fields and task states separate, retrieve the video, use callbacks and external IDs, and reconcile per-second charges against reported usage and billing.
Contents

Kling API text-to-video returns a task, not a finished file. Developers adding video generation to applications must save its ID, poll its status, download the result, and reconcile billing. Start with the five-second BetterToken example below, or use the official Kling 4.0 request schema for 4K and HDR settings.
Choose an API route before buying credits
Check API funding even when you already use Kling’s website. As of 2026-11-27, the Kling 4.0 access requirements specify a Flagship resource pack or a positive cash balance. Sharing a web login does not establish that funding. Check account eligibility and available payment options in your chosen console before purchasing.
| Route | What you need and how usage is funded | When to choose it |
|---|---|---|
| Kling website or app | A web account and any purchase required for the selected interactive feature | Manual creation rather than an application integration |
| Official Kling API | A developer account and API resources. Kling 4.0 requires a Flagship resource pack or positive cash balance; calls consume API Units or cash | Official 4.0 parameters, callbacks, and vendor billing records |
| BetterToken API | Your own BetterToken account, API key, and sufficient balance. Video charges follow the selected model’s billing unit | A quote-before-create workflow with a common video task interface |
Generate a quoted Kling job through BetterToken
Create your own API key in the workspace. The Base URL is https://www.bettertoken.ai/v1; supply the key through Authorization: Bearer. The Kling API access page provides the product entry point.
This example follows the documented Kling request for kling-3.0-turbo. Its text-to-video operation accepts 5 or 10 seconds, with audio on. Use this example to establish the submission and billing workflow; it does not provide Kling 4.0’s HDR controls.
Run these commands in Bash, with curl, jq, and uuidgen installed. Use a new empty directory for each intentionally new generation.
set -euo pipefail
umask 077
read -rs BETTERTOKEN_API_KEY && export BETTERTOKEN_API_KEY
BASE='https://www.bettertoken.ai/v1'
cat > request.json <<'JSON'
{
"model": "kling-3.0-turbo",
"operation": "text_to_video",
"prompt": "A ceramic cup on a wooden table. The camera slowly moves closer.",
"duration": {"mode": "fixed", "seconds": 5},
"output": {"resolution": "720p", "aspect_ratio": "16:9"},
"audio": {"mode": "on"}
}
JSON
curl --fail-with-body --max-time 120 "$BASE/videos/quote" \
-H "Authorization: Bearer $BETTERTOKEN_API_KEY" \
-H 'Content-Type: application/json' \
--data-binary @request.json -o quote.json
jq '{currency, reserved_amount, max_charge_amount, expires_at}' quote.json
Review the quote before continuing. It does not charge, reserve funds, or create a task, and it is not the final charge.
Run the next block in the same Bash session. It creates a paid task. Keep request.json and idempotency-key.txt unchanged when recovering the same submission. The video API’s retry rules warn that retrying without the same Idempotency-Key creates another charged task.
set -euo pipefail
read -r -p 'Type GENERATE to create a paid task: ' CONFIRM
[[ "$CONFIRM" == GENERATE ]] || exit 1
[[ -s idempotency-key.txt ]] || uuidgen > idempotency-key.txt
curl --fail-with-body --max-time 120 "$BASE/videos" \
-H "Authorization: Bearer $BETTERTOKEN_API_KEY" \
-H "Idempotency-Key: $(cat idempotency-key.txt)" \
-H 'Content-Type: application/json' \
--data-binary @request.json -o created.json
TASK_ID=$(jq -er '.id' created.json)
for ((n=0; n<120; n++)); do
curl --fail-with-body --max-time 60 "$BASE/videos/$TASK_ID" \
-H "Authorization: Bearer $BETTERTOKEN_API_KEY" -o task.json
STATUS=$(jq -er '.status' task.json)
case "$STATUS" in
failed|canceled) jq '{id, status, error, billing}' task.json >&2; exit 1 ;;
esac
if jq -e '.status == "completed" and .delivery_status == "ready"' task.json >/dev/null; then
break
fi
sleep 5
done
jq -e '.status == "completed" and .delivery_status == "ready"' task.json >/dev/null || {
printf '%s\n' 'Not ready: inspect task.json and resume this task ID.' >&2
exit 1
}
: > kling.mp4.part
curl --fail-with-body --max-time 300 "$BASE/videos/$TASK_ID/content" \
-H "Authorization: Bearer $BETTERTOKEN_API_KEY" -o kling.mp4.part
test -s kling.mp4.part
mv -- kling.mp4.part kling.mp4
jq '.billing' task.json
Open kling.mp4 to confirm playback. Reconcile the completed task’s billing, not the quote; a missing amount is not proof of zero charges. The task-query reference documents failed, canceled, and the task-level error object used above.
The polling interval and attempt count are example client limits, not a generation-time promise. After an interruption, resume the saved task instead of starting another generation.
Match the model ID to the operation
The current catalog lists these Kling IDs as of 2026-11-27. The official paths identify the corresponding model families; they do not describe BetterToken’s private upstream routing. BetterToken requests use /v1/videos.
| BetterToken model ID | Corresponding family and official endpoint | Gateway detail that changes your request |
|---|---|---|
kling-video-v4 | Kling 4.0: /text-to-video/kling-4.0 | Catalog lists text-to-video and reference-to-video, 3–30 seconds, at 720p or 1080p |
kling-v3 | Kling 3.0: /text-to-video/kling-3.0 | Text or image input; output mode uses provider_options.kling.mode |
kling-3.0-turbo | Kling 3.0 Turbo: /text-to-video/kling-3.0-turbo | The gateway documents 5 or 10 seconds, with audio on |
kling-v3-omni | Kling 3.0 Omni: /omni-video/kling-3.0-omni | The documented gateway operation is text-to-video |
kling-v3-motion-control | Motion Control 3.0: /motion-control/kling-3.0 | Requires character-image and motion-video references; duration follows the reference |
The gateway’s model-specific guide defines its request fields. The official 3.0, Turbo, Omni, and Motion Control APIs use their own bodies. Do not transplant one interface’s JSON into the other.
For kling-video-v4, inspect its authenticated GET /v1/video-models entry before quoting. The public listing does not establish gateway support for 4K, HDR, or a Flash selector. Official Flash uses /text-to-video/kling-4.0-flash; do not treat it as an interchangeable name for 4.0.
Call Kling 4.0 directly
Follow Kling’s official quick start to fund API usage and create a key. Its international console uses email login. Select + Create a new API Key, name it, and store the value when shown; it appears only once. For servers outside China, the authentication documentation specifies https://api-singapore.klingai.com.
Use the API key directly in the Bearer header. This Bash example requests five seconds at 1080p and saves a unique application identifier for later lookup.
set -euo pipefail
umask 077
read -rs KLING_API_KEY && export KLING_API_KEY
KLING_BASE='https://api-singapore.klingai.com'
EXTERNAL_TASK_ID=$(uuidgen)
jq -n --arg external_id "$EXTERNAL_TASK_ID" '{
prompt: "A ceramic cup on a wooden table. The camera slowly moves closer.",
settings: {resolution: "1080p", duration: 5},
options: {external_task_id: $external_id}
}' > kling-request.json
curl --fail-with-body --max-time 120 "$KLING_BASE/text-to-video/kling-4.0" \
-H "Authorization: Bearer $KLING_API_KEY" \
-H 'Content-Type: application/json' \
--data-binary @kling-request.json -o kling-created.json
jq -e '.code == 0' kling-created.json >/dev/null
jq -er '.data.id | strings | select(length > 0)' kling-created.json \
> kling-task-id.txt.part
mv -- kling-task-id.txt.part kling-task-id.txt
In the 4.0 request schema, prompt is required. settings.duration accepts integer seconds from 3 to 30. An explicit value overrides duration mentioned in the prompt; without either, the default is 10 seconds.
Set resolution through settings.resolution: 720p, 1080p, or 4k. HDR requires settings.dynamic_range: "hdr" with 1080p or 4k; otherwise SDR is the default. Writing “4K HDR” in the prompt does not activate either setting.
Poll GET /tasks and download the result
The official task-query reference defines submitted, processing, succeeded, and failed. Official success is succeeded, not BetterToken’s completed. A failed task reports its reason in the task-level message field.
Run the following block from the same job directory, including in a fresh Bash session. Paste your API key again when read waits for input. The block reloads the host and saved ID; it does not create another task.
set -euo pipefail
umask 077
read -rs KLING_API_KEY && export KLING_API_KEY
KLING_BASE='https://api-singapore.klingai.com'
TASK_ID=$(cat kling-task-id.txt)
[[ -n "$TASK_ID" ]] || exit 1
for ((n=0; n<120; n++)); do
curl --fail-with-body --max-time 60 --get "$KLING_BASE/tasks" \
-H "Authorization: Bearer $KLING_API_KEY" \
-H 'Content-Type: application/json' \
--data-urlencode "task_ids=$TASK_ID" -o kling-task.json
jq -e --arg id "$TASK_ID" '
.code == 0
and (.data | type == "array")
and (.data | length == 1)
and (.data[0].id == $id)
' kling-task.json >/dev/null
case "$(jq -r '.data[0].status' kling-task.json)" in
succeeded) break ;;
failed) jq '.data[0] | {id, status, message, billing}' kling-task.json >&2; exit 1 ;;
submitted|processing) sleep 5 ;;
*) jq '.data[0] | {id, status, message}' kling-task.json >&2; exit 1 ;;
esac
done
jq -e '.data[0].status == "succeeded"' kling-task.json >/dev/null || {
printf '%s\n' 'Still pending: resume polling this task ID.' >&2
exit 1
}
VIDEO_URL=$(jq -er '
[.data[0].outputs[] | select(.type == "video") | .url][0]
| strings | select(length > 0)
' kling-task.json)
: > kling-official.mp4.part
curl --fail-with-body --location --max-time 300 "$VIDEO_URL" \
-o kling-official.mp4.part
test -s kling-official.mp4.part
mv -- kling-official.mp4.part kling-official.mp4
jq '.data[0] | {id, status, usage, billing}' kling-task.json
A failed request or response validation stops this block. The block replaces the final MP4 only after a successful, nonempty download. Open it to verify playback.
The query returns a data array. Video URLs appear in each task’s outputs array, in objects whose type is video. The download deliberately omits your API key. The usage object appears after the task ends and usage becomes available; its absence does not mean zero usage.
Use callbacks and external IDs without losing the system ID
Set options.callback_url to your HTTPS receiver when creating a task. Kling sends notifications when task status changes. Configure signature verification through the callback protocol; its Webhook Secret is separate from the API key. Keep polling available for reconciliation.
options.external_task_id must be unique within your account. It does not replace data.id. The task-query reference accepts either task_ids or external_task_ids, never both; comma-separated values allow batch queries.
After an interrupted creation request, run this lookup from the saved request’s directory. It reloads the key and host, then saves the matched system ID for the polling block.
set -euo pipefail
umask 077
read -rs KLING_API_KEY && export KLING_API_KEY
KLING_BASE='https://api-singapore.klingai.com'
EXTERNAL_TASK_ID=$(jq -er '
.options.external_task_id | strings | select(length > 0)
' kling-request.json)
curl --fail-with-body --max-time 60 --get "$KLING_BASE/tasks" \
-H "Authorization: Bearer $KLING_API_KEY" \
-H 'Content-Type: application/json' \
--data-urlencode "external_task_ids=$EXTERNAL_TASK_ID" \
-o kling-lookup.json
jq -e --arg external_id "$EXTERNAL_TASK_ID" '
.code == 0
and (.data | type == "array")
and (.data | length == 1)
and (.data[0].external_id == $external_id)
' kling-lookup.json >/dev/null
jq -er '.data[0].id | strings | select(length > 0)' kling-lookup.json \
> kling-task-id.txt.part
mv -- kling-task-id.txt.part kling-task-id.txt
jq '.data[0] | {id, status, message}' kling-lookup.json
If no matching task is returned, the block stops without replacing the saved ID. Inspect kling-lookup.json before deciding whether to submit again. An external ID supports lookup; it is not a documented guarantee that repeated creation requests are deduplicated.
Reconcile per-second charges with reported usage
For official Kling 4.0, calculate with usage.output_duration, not seconds written in your prompt or the file’s outputs[].duration. For jobs with video input, use the reported usage.input_duration too. The deduction schema distinguishes actual file duration from billable output duration.
Kling’s international pricing table shows these cash list rates as of 2026-11-27, in USD per second. Resource-pack Units are a separate accounting unit. Let O be billable output seconds and I billable input seconds.
| Resolution | No input video | With input video, I ≤ O | With input video, I > O | Additional HDR rate |
|---|---|---|---|---|
720p | $0.157/s | $0.094/s | $0.094/s | Not available |
1080p | $0.235/s | $0.141/s | $0.141/s | $0.047/output second |
4k | $0.549/s | $0.329/s | $0.329/s | $0.078/output second |
The input-video rates match across both duration cases, but their billable-duration formulas differ. Select the rate for the resolution and input condition, then calculate:
- No input video:
rate × O. - Input video with
I ≤ O:rate × O × 2. - Input video with
I > O:rate × (I + O).
Add HDR rate × O when HDR is enabled. Input-video duration includes reference videos and video character elements. These input-video rules concern reference-based jobs, not the text-only request above.
Calculated examples at the published cash list rates:
| Workload | Calculation | List-rate amount |
|---|---|---|
| Five-second 1080p SDR, no input video | 5 × $0.235 | $1.175 |
| Six-second input, five-second output, 4K HDR | (6 + 5) × $0.329 + 5 × $0.078 | $4.009 |
These are calculations, not observed charges. Reconcile them with the task response’s billing records and your account’s applicable terms. Keep charge_type: "cash" amounts and their currency separate from charge_type: "unit" deductions. A task can contain several billing records.
For repeated generation, track all charges against the number of accepted clips. The cost-per-accepted-video guide covers retries. The task-cost and elapsed-time comparison method provides a framework for comparing providers on completed work.
Recover the existing job before creating another
When polling stops, restore the saved task ID and query it again. A client timeout does not establish that generation failed. For BetterToken, retain the original request and Idempotency-Key when recovering creation; changing either is not the same retry.
For official Kling HTTP 429 responses, inspect the service code. 1102 identifies an exhausted or expired prepaid resource pack. 1303 identifies concurrency or QPS exceeding the prepaid package limit. Restore API resources for the first case, or reduce request frequency and concurrent work for the second. Check the official error table rather than treating every 429 as a temporary queue.
A task marked failed needs its diagnostic reviewed, even when the polling HTTP request succeeded. Inspect BetterToken’s task-level error, or official Kling’s task-level message. Preserve the request, task ID, status, usage, and billing evidence before deciding whether a corrected request should become a new paid job.