快速开始
准备 API Key;默认连接 https://api.deepraxis.com,可用 PRAXIS_API_BASE 覆盖。将下方代码保存为 quickstart.mjs,在 Node.js 中运行。示例建库、默认私有上传、按标签检索,验证匿名读取 404、授权读取 200,再显式公开一张图片并验证匿名 200。
# Node.js 18+ or Bun; the example uses fetch and a data URI.# Set PRAXIS_API_KEY in your environment.node quickstart.mjsimport { writeFile } from "node:fs/promises";
const base = process.env.PRAXIS_API_BASE ?? "https://api.deepraxis.com";const key = process.env.PRAXIS_API_KEY;if (!key) throw new Error("Set PRAXIS_API_KEY");async function media(path, method = "GET", body) { const response = await fetch(base + "/media/v1" + path, { method, headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }, ...(body === undefined ? {} : { body: JSON.stringify(body) }), }); if (!response.ok) throw new Error(`Media HTTP ${response.status}: ${await response.text()}`); return response.json();}// A library isolates its assets, folders and searches.const library = await media("/libraries", "POST", { name: "Agent media", understanding: { embedding: true } });const asset = await media(`/libraries/${library.id}/assets`, "POST", { file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAABWklEQVR4nO3OQQ0AMBAEofVv+iqDxzRBALvtg/wgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwg7gEgaMOyrMtNTwAAAABJRU5ErkJggg==", public_id: "quickstart/pixel", tags: ["docs"],});if (asset.type !== "authenticated") throw new Error("New uploads must be private");if (asset.material_type !== "image") throw new Error(`Expected material_type image, got ${asset.material_type}`);// One type axis: material_type. Audio is audio, and audio_role tells speech, music and sound effects apart.const images = await media(`/libraries/${library.id}/search/expression`, "POST", { expression: "material_type:image AND tags=docs",});if (images.resources.length !== 1) throw new Error("Expression search did not find the upload");// Store this reference, never a signed URL or token.const reference = { library_id: library.id, asset_id: asset.asset_id, version: asset.version };const { token } = await media(`/libraries/${library.id}/delivery_tokens`, "POST", { acl: `/${library.id}/image/authenticated/*`, expires_in: 900,});const publicId = asset.public_id.split("/").map(encodeURIComponent).join("/");const original = new URL(`/${library.id}/image/authenticated/v${asset.version}/${publicId}.${asset.format}`, asset.secure_url);const thumbnail = new URL(original);thumbnail.pathname = thumbnail.pathname.replace("/authenticated/", "/authenticated/w_160,c_fit/");async function expectStatus(url, expected) { const response = await fetch(url); if (response.status !== expected) throw new Error(`Expected ${expected}, got ${response.status}`); const body = Buffer.from(await response.arrayBuffer()); if (expected === 200 && body.length === 0) throw new Error("Delivery returned an empty body");}await expectStatus(original, 404);await expectStatus(thumbnail, 404);original.searchParams.set("__cld_token__", token);thumbnail.searchParams.set("__cld_token__", token);await expectStatus(original, 200);await expectStatus(thumbnail, 200);// Catalogue images and avatars require an explicit public upload.const published = await media(`/libraries/${library.id}/assets`, "POST", { file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAABWklEQVR4nO3OQQ0AMBAEofVv+iqDxzRBALvtg/wgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwg7gEgaMOyrMtNTwAAAABJRU5ErkJggg==", public_id: "quickstart/public-pixel", type: "upload",});if (published.type !== "upload") throw new Error("Expected public delivery");await expectStatus(published.secure_url, 200);const current = await media(`/libraries/${library.id}/assets/${asset.asset_id}`);const matches = current.semantic === "ready" ? await media(`/libraries/${library.id}/search/semantic`, "POST", { query: "a small image", top_k: 5 }) : null;// Print the reference and the counts, never a token or a token-bearing link: those are per-read and not stored.console.log({ reference, public_asset_id: published.asset_id, found_count: images.resources.length, semantic: current.semantic, matches_count: matches?.hits?.length ?? 0 });投递令牌默认有效 900 秒,取帧链接有效 300 秒;过期后按素材引用重新获取。长期保存 library_id、asset_id、version 与所需表示,不保存令牌或带令牌链接,也不要记录到日志。链接是持有者凭据,只交给预期读者。
理解异步完成。上传响应不含处理状态;用 GET 读取素材,查看 processing 与 semantic。量级参考:图片约半分钟,3 秒视频约 50 秒,raw 文件立即就绪(不做理解)。首次读取可能为空;matches 为 null 时稍后重新读取素材,semantic 为 ready 后再检索。理解与语义检索可用;上传、表达式检索和原文件读取无需等待理解完成。
示例保留库供继续使用。用完后,把 LIBRARY_ID 设为输出的 reference.library_id 并删除:
curl -X DELETE "${PRAXIS_API_BASE:-https://api.deepraxis.com}/media/v1/libraries/${LIBRARY_ID}" \ -H "Authorization: Bearer $PRAXIS_API_KEY"在控制台里
不写代码也可以:打开控制台 → 素材库(/media),点「新建素材库」建库;进入这个库,点「上传素材」选文件上传。