Skip to content

Uploading media

There are four ways to get a file into HalationLab.ai. All of them end with an asset in a project.

Your file is Use Limit
Online Its URL, directly in a generation’s input As for an import
Online POST /v1/assets with url Up to 100 MB; the server must answer within 20 seconds
Small and local POST /v1/assets with data (a data URI) About 700 KB; request bodies are limited to 1 MiB
Large and local POST /v1/uploads, then PUT the bytes 16 MiB per PUT; larger files are sent in 16 MiB parts

Supported files: JPEG, PNG and WebP images; MP4, MOV and WebM video; MP3, WAV and M4A audio. Other types are refused with 415 unsupported_media_type.

import-url.sh
# Imports a file from a public HTTPS URL into the default project. The asset is `processing` until it is checked.
set -euo pipefail
API="${HL_API_BASE:-https://api.halationlab.ai/v1}"
IMAGE="${HL_SAMPLE_IMAGE_URL:-https://docs.halationlab.ai/media/sample-image.png}"
curl -sS --fail-with-body "$API/assets" \
-H "Authorization: Bearer $HALATIONLAB_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d "{ \"url\": \"$IMAGE\", \"name\": \"Sample image\" }"

The URL must be a public HTTPS URL. The answer is the asset with status: "processing" while the file is checked, usually a few seconds; then it is ready or failed. Read it with GET /v1/assets/{id}. A URL that cannot be fetched is 422 media_fetch_failed.

You can also put the URL straight into a generation’s input. It is imported first; if the check takes longer than about 20 seconds, the generation request answers 409 media_not_ready with Retry-After. Send the identical request again with the same Idempotency-Key and it continues where it stopped.

import-data-uri.sh
# Sends a small local file inline as a data URI (up to about 700 KB). Use an upload for anything larger.
set -euo pipefail
API="${HL_API_BASE:-https://api.halationlab.ai/v1}"
FILE="${1:-$(dirname "$0")/../../public/media/sample-image.png}"
data="data:image/png;base64,$(base64 < "$FILE" | tr -d '\n')"
curl -sS --fail-with-body "$API/assets" \
-H "Authorization: Bearer $HALATIONLAB_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d @- <<JSON
{ "data": "$data", "name": "Inline sample" }
JSON
  1. POST /v1/uploads with name, content_type, size and sha256 (the SHA-256 of the whole file, as 64 lower-case hex characters). The upload URLs only accept these exact bytes.
  2. PUT each part to its url, with the headers given, exactly as given. A file up to 16 MiB is one PUT; a larger file is sent in parts of part_size bytes. Keep each part’s ETag response header.
  3. POST /v1/uploads/{id}/complete, with {"parts": [{"part_number", "etag"}]} for a multi-part upload, or {} for a single PUT.
  4. Read GET /v1/uploads/{id} until status is completed, then use its asset. It is processing while the file is checked.

The upload’s parts may list only the next parts to send. GET /v1/uploads/{id} issues fresh URLs for the parts not yet received, and received_parts lists those that arrived, so an interrupted upload can resume. An unfinished upload is discarded at expires_at.

With the Python standard library:

upload.py
# Uploads a local file of any size with the standard library: start the upload with its SHA-256, PUT each part,
# complete it, then wait until its asset is ready. Usage: python3 upload.py path/to/file
import hashlib
import json
import math
import mimetypes
import os
import sys
import time
import urllib.request
import uuid
API = os.environ.get("HL_API_BASE", "https://api.halationlab.ai/v1")
KEY = os.environ["HALATIONLAB_API_KEY"]
DEFAULT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "public", "media", "sample-image.png")
path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT
def call(method: str, route: str, body: dict | None = None) -> dict:
headers = {"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"}
if method != "GET":
headers["Idempotency-Key"] = str(uuid.uuid4())
data = json.dumps(body).encode() if body is not None else None
request = urllib.request.Request(f"{API}{route}", data=data, method=method, headers=headers)
with urllib.request.urlopen(request, timeout=60) as response:
return json.load(response)
with open(path, "rb") as f:
content = f.read()
upload = call("POST", "/uploads", {
"name": os.path.basename(path),
"content_type": mimetypes.guess_type(path)[0] or "application/octet-stream",
"size": len(content),
"sha256": hashlib.sha256(content).hexdigest(),
})
# One PUT up to 16 MiB; larger files use parts of `part_size` bytes. The response may list only the next parts:
# reading the upload again issues URLs for the parts still missing.
part_size = upload["part_size"]
total = math.ceil(len(content) / part_size) if part_size else 1
etags: dict[int, str] = {}
while len(etags) < total:
for part in upload["parts"]:
n = part["part_number"]
if n in etags:
continue
chunk = content[(n - 1) * part_size: n * part_size] if part_size else content
put = urllib.request.Request(part["url"], data=chunk, method="PUT", headers=part["headers"])
with urllib.request.urlopen(put, timeout=300) as response:
etags[n] = response.headers["ETag"]
if len(etags) < total:
upload = call("GET", f"/uploads/{upload['id']}")
parts = [{"part_number": n, "etag": etag} for n, etag in sorted(etags.items())]
upload = call("POST", f"/uploads/{upload['id']}/complete", {"parts": parts} if part_size else {})
# The file is checked before it can be used; this usually takes seconds.
while upload["status"] == "processing":
time.sleep(2)
upload = call("GET", f"/uploads/{upload['id']}")
if upload["status"] != "completed":
sys.exit(f"Upload {upload['status']}: {upload['error']}")
print("asset", upload["asset"])

The SDKs do all of this in one call:

upload-sdk.ts
// Uploads a local file with the TypeScript SDK, which hashes it, sends the parts and waits for the asset.
import { HalationLab } from '@halationlab/sdk';
const hl = new HalationLab({ apiKey: process.env.HALATIONLAB_API_KEY });
const asset = await hl.assets.upload(process.argv[2] ?? 'product.png');
console.log(asset.id, asset.status);
upload-sdk.py
# Uploads a local file with the Python SDK, which hashes it, sends the parts and waits for the asset.
import os
import sys
from halationlab import HalationLab
client = HalationLab(api_key=os.environ["HALATIONLAB_API_KEY"])
asset = client.assets.upload(sys.argv[1] if len(sys.argv) > 1 else "product.png")
print(asset["id"], asset["status"])

Pass the asset ID as a media input, for example "first_frame": "asset_…". Generated outputs are assets too: pass outputs[0].asset, or the generation ID for its first output.

Asset download URLs need no credentials and expire at url_expires_at (one hour). Read the asset again for a fresh URL. url is null unless status is ready.

POST /v1/assets/{id}/transform makes a copy with crop (a rectangle in source pixels), trim (start and end in seconds, for video and audio) or resize (width and height in pixels), or a combination. The copy is a new asset with origin: "derived".

{ "crop": { "x": 0, "y": 140, "width": 1080, "height": 1080 } }

DELETE /v1/assets/{id} moves an asset to the account’s trash, from which the owner can restore it in the app for 30 days.