Create a media-library upload session
/v1/asset-upload-sessionsconst { readFile } = await import("node:fs/promises");
const { createHash, randomUUID } = await import("node:crypto");
const data = await readFile("invoice.pdf");
const upload = await bird.assets.uploads.create({
client_reference: randomUUID(),
file: {
filename: "invoice.pdf",
content_type: "application/pdf",
size_bytes: data.length,
sha256: createHash("sha256").update(data).digest("hex"),
},
});
console.log(upload.id);import hashlib
import uuid
from pathlib import Path
data = Path("invoice.pdf").read_bytes()
upload = client.assets.uploads.create(
client_reference=str(uuid.uuid4()),
file={
"filename": "invoice.pdf",
"content_type": "application/pdf",
"size_bytes": len(data),
"sha256": hashlib.sha256(data).hexdigest(),
},
)
print(upload.id)data, err := os.ReadFile("invoice.pdf")
if err != nil {
log.Fatal(err)
}
sum := sha256.Sum256(data)
// Keep this reference and the same input when retrying this upload.
reference := uuid.NewString()
upload, err := client.Assets.Uploads.Create(context.Background(), bird.AssetsUploadsCreateParams{
ClientReference: reference,
File: bird.AssetUploadFile{Filename: "invoice.pdf", ContentType: "application/pdf", SizeBytes: int64(len(data)), Sha256: hex.EncodeToString(sum[:])},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(upload.Id)$data = file_get_contents('invoice.pdf');
if ($data === false) {
throw new RuntimeException('Cannot read invoice.pdf');
}
$file = new AssetUploadFile();
$file->setFilename('invoice.pdf');
$file->setContentType('application/pdf');
$file->setSizeBytes(strlen($data));
$file->setSha256(hash('sha256', $data));
$params = new AssetUploadSessionCreate();
$params->setClientReference('4ca4145c-782a-4d03-bc80-a27e3e431780');
$params->setFile($file);
$upload = $bird->assets->uploads->create($params);
echo $upload->getId();bird assets uploads create --body-file - <<'JSON'
{
"client_reference": "4ca4145c-782a-4d03-bc80-a27e3e431780",
"file": {
"filename": "invoice.pdf",
"content_type": "application/pdf",
"size_bytes": 1024,
"sha256": "4e2090a479f7ce6b17ad31f991988f12571bcafbd4a441cc4063644fc1400ae0"
},
"folder_id": "asf_01krdgeqcxet5s7t44vh8rt9mg"
}
JSONcurl -X POST "https://us1.platform.bird.com/v1/asset-upload-sessions" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"client_reference": "4ca4145c-782a-4d03-bc80-a27e3e431780",
"file": {
"filename": "invoice.pdf",
"content_type": "application/pdf",
"size_bytes": 1024,
"sha256": "4e2090a479f7ce6b17ad31f991988f12571bcafbd4a441cc4063644fc1400ae0"
},
"folder_id": "asf_01krdgeqcxet5s7t44vh8rt9mg"
}'{
"id": "aus_01krdgeqcxet5s7t44vh8rt9mg",
"state": "pending",
"folder_id": "asf_01krdgeqcxet5s7t44vh8rt9mg",
"file": {
"filename": "invoice.pdf",
"content_type": "application/pdf",
"size_bytes": 1024,
"sha256": "4e2090a479f7ce6b17ad31f991988f12571bcafbd4a441cc4063644fc1400ae0"
},
"result": {
"asset_id": "ast_01krdgeqcxet5s7t44vh8rt9mg"
},
"next": [
{
"kind": "operation"
}
]
}
Creates or recovers a session. An upload instruction is omitted when the session is no longer pending or another instruction was issued within the last 30 seconds; follow the returned state and next steps. Repeating the same client reference with identical input recovers its original session. A changed request with that reference is refused. Upload instructions can be refreshed after 30 seconds. Creating a session does not publish the file. Completion publishes it at a permanent public URL.
In each region, you can have up to 20 unexpired sessions per workspace, 100 per organization, and 500 MiB of reserved source bytes per organization. A new session reserves file.size_bytes; a request that would exceed a limit returns 429 with E20021. Replaying an existing session does not reserve capacity again.
Sessions count toward the session limits until their one-hour expiry, including completed and cancelled sessions. Completing, cancelling, or expiring a session does not immediately release its reserved bytes: issued upload URLs must expire and cleanup must succeed. Retry new creates after capacity becomes available.
Payload Permintaan
client_referenceCaller-generated UUID for this upload intent. Retries with identical input recover the same session while its receipt is retained.
fileExact properties of the original file. Calculate size and SHA-256 from its bytes before creating a session. Images allow up to 5,242,880 bytes; other supported media allow up to 25,000,000 bytes.
nameDisplay name. Omitted or blank names default to the sanitized filename.
alt_textDefault alternative text for the asset.
folder_idDestination folder in this workspace. Omit it to use the library root.
Payload Respons
idstateUpload progress. Pending accepts a transfer; finalizing is checking and publishing it. Completed has a saved result. Rejected, cancelled and expired allow no new upload instruction or publication. An already issued upload URL can remain usable until its expiry.
Possible values: pending, finalizing, completed, rejected, cancelled, expired
expires_atAbsolute session expiry. Completion must finish before this deadline; starting it or refreshing an upload instruction does not extend it.
nameFrozen display name for a newly published asset.
alt_textFrozen alternative text, or null when omitted.
folder_idFrozen destination folder, or null for the library root.
fileExact properties of the original file. Calculate size and SHA-256 from its bytes before creating a session. Images allow up to 5,242,880 bytes; other supported media allow up to 25,000,000 bytes.
resultSaved publication receipt, or null until completed.
last_errorLast completion error, or null when no failure is recorded.
nextAvailable next steps for this session.
Tampilkan atribut turunan
next.kindWhat you do about this step.
operation: call the operation named inoperation, then read again.external: act somewhere this API does not reach, then read again.wait: nothing is asked of you, so read again later.terminal: nothing you do resolves this, so stop retrying.
Tolerate a value you do not recognize: show the description and
offer no action.
Possible values (may grow over time): operation, external, wait, terminal
next.descriptionA short, human-readable label for the step, suitable for display.
next.operationThe operationId to call. Present only when kind is operation. The operation's own schema says how to call it; this says only which one, and what to address it with.
next.paramsThe parameters that address the operation, by name: {"sender_id": "…"} for an operation on /v1/sms/senders/{sender_id}/requirements. A parameter the operation takes in its query string is given the same way, so an operation addressed as ?subject_id= carries {"subject_id": "…"}. Every parameter the call needs is here, whether its value came from the thing you were acting on or is fixed for this step, so you can make the call from this object alone. Present only when kind is operation and the operation names a subject. A request body, when the operation takes one, is described by the operation's own schema and never appears here.
next.urlA URL to open. Present only when kind is external, and only when the step has one. An external step whose description says to go and do something with no URL to open is normal.
uploadSend the original file bytes to this URL with the exact headers and content length. Send no Bird credentials or cookies. Keep the URL private and call complete after the PUT succeeds.
Sumber daya terkait
Lanjutkan dengan dokumentasi, panduan, dan contoh untuk topik ini.