Where this fits
Python can connect a script, worker or web backend to Knightvision’s REST API. The example below submits one text-to-video request and checks the resulting job. Run it only when you intend to spend generation credits; local tests of the example use mocked HTTP responses instead of paid generation.
Plan the approach
Install requests in your project environment and set KNIGHTVISION_API_KEY through your normal secret configuration. The example uses a bounded polling loop and writes the accepted job ID to a local record before waiting. For a production service, replace that file with an owned database record and resume status checking from the saved ID.
A practical workflow
- Review the model parameters and check your account balance before running the paid example.
- Submit once, persist the returned job_id and poll only that accepted job.
- Handle complete, error and the polling deadline separately; a deadline means resume later, not submit again.
WORKED EXAMPLE / ILLUSTRATIVE BRIEF
Put the idea into practice
Example operation: A server-side content job requests a five-second 720p Kling scene. When the job completes, the worker records the video URL for an editor to review.
Python example
This example has been checked with mocked responses. Running it against your account submits a paid generation. It resumes the job saved in knightvision-video-job.json; use a durable project database for a production service. Reconcile an uncertain submission before starting a new job.
# Python 3; install requests in your project environment.
# Set KNIGHTVISION_API_KEY securely before running. This submits a paid job.
import json
import os
import time
from pathlib import Path
import requests
BASE = "https://knightvision.ai/api/v1/partner"
RECORD = Path("knightvision-video-job.json")
HEADERS = {"Authorization": "Bearer " + os.environ["KNIGHTVISION_API_KEY"]}
PAYLOAD = {
"model": "kling-v3-text-to-video",
"prompt": "A gentle camera move across a quiet coastal road at sunrise",
"duration": 5,
"quality": "720p",
"aspect_ratio": "16:9",
"sound": False,
}
def run():
if RECORD.exists():
saved = json.loads(RECORD.read_text())
if saved["state"] == "submitting":
raise RuntimeError("Submission uncertain: check /videos before retrying.")
job_id = saved["job_id"]
else:
# Exclusive creation prevents two copies submitting from this directory.
with RECORD.open("x") as record:
json.dump({"state": "submitting", "payload": PAYLOAD}, record)
# Do not automatically retry POST after an ambiguous network failure.
response = requests.post(BASE + "/generate-video", headers=HEADERS,
json=PAYLOAD, timeout=60)
response.raise_for_status()
job_id = response.json()["job_id"]
RECORD.write_text(json.dumps({"state": "accepted", "job_id": job_id}))
deadline = time.monotonic() + 600
while time.monotonic() < deadline:
response = requests.get(BASE + "/video-status/" + job_id,
headers=HEADERS, timeout=30)
response.raise_for_status()
result = response.json()
if result["status"] == "complete":
print(result["video_url"])
return result["video_url"]
if result["status"] == "error":
raise RuntimeError("Generation failed; inspect the saved job in your workspace.")
time.sleep(5)
raise TimeoutError("Still pending; rerun to resume the saved job, not submit again.")
if __name__ == "__main__":
run()
WATCH AN EXISTING SHOWCASE
From an accepted job to a viewable clip
Build your version
Install requests in a virtual environment, set your API key outside the source file, and run the example from a dedicated project directory. It saves the accepted job ID. Running it again resumes polling the same job instead of submitting another paid generation.
The following is a suggested workflow, not the original generation record for the showcase.
Review the result
- Test complete, error and pending responses before a production rollout.
- If submission is uncertain, reconcile job history before deleting the saved record or retrying.
- Store approved output and request metadata together, while keeping credentials out of both.
Plan the cost
The code requests a five-second Kling 3.0 clip at 720p with sound off. The current API documentation lists 55 credits for that configuration. Confirm the current account rate with GET /credits; this is not a measured cost of the displayed clip.
Check current API credits and pricing. Record model, settings, job ID, credits charged and accepted output for each new attempt.
What to check
Keep secrets out of stdout and tracebacks shared with users. Test network failures, invalid responses and error status before adding the script to an unattended process.
What should I do if the script reaches its polling deadline?
The job may still be running. Use the saved job ID to check it later. Do not generate another copy simply because the local script stopped waiting.
Tools and documentation
Use these product references for current settings and availability.