Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32# STATUS: LIVE_VERIFIED — executed 2026-09-19 (exit 0 in 33.2s)3# Collections on api.x.ai with the inference key (LIVE_DISCOVERED; docs say management-api.x.ai + Management key):4# create -> upload file -> add document (body must repeat collection_id + file_id) -> poll status -> documents/search -> cleanup.5# Search returns 404 "not found or not accessible" until the document is indexed (took > 75 s in probes), so this example tolerates it.6import io, json, os, pathlib, sys, time7ROOT = pathlib.Path(__file__).resolve().parents[3]; sys.path.insert(0, str(ROOT))8from scripts import live # noqa: E4029X = live.xai_request10st, col, _ = X("POST", "/v1/collections", {"collection_name": "atlas-example", "field_definitions": [], "collection_description": "atlas example"}, note="example xai/collections/lifecycle.py create")11assert st == 200, (st, col); cid = col["collection_id"]; print("collection", cid, col["index_configuration"])12bnd = "----atlas"; body = (f"--{bnd}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"atlas-coll.txt\"\r\nContent-Type: text/plain\r\n\r\n").encode() + b"The secret fruit is MANGO. " * 8 + f"\r\n--{bnd}--\r\n".encode()13st, f, _ = X("POST", "/v1/files", data=body, content_type=f"multipart/form-data; boundary={bnd}", note="example xai/collections/lifecycle.py upload"); assert st == 200; fid = f["id"]14st, b, _ = X("POST", f"/v1/collections/{cid}/documents/{fid}", {"collection_id": cid, "file_id": fid, "fields": {}}, note="example xai/collections/lifecycle.py add document"); print("add document:", st, b)15status = None16for _ in range(6):17 st, d, _ = X("GET", f"/v1/collections/{cid}/documents/{fid}", note="example xai/collections/lifecycle.py poll"); status = d.get("status") if st == 200 else d18 if status in ("DOCUMENT_STATUS_PROCESSED", 2): break19 time.sleep(5)20print("document status:", status, "(1 = PROCESSING, docs say string enum)")21st, s, _ = X("POST", "/v1/documents/search", {"query": "secret fruit", "source": {"collection_ids": [cid]}, "limit": 2}, est_cost_usd=0.001, note="example xai/collections/lifecycle.py search")22print("search:", st, json.dumps(s)[:200])23X("DELETE", f"/v1/collections/{cid}/documents/{fid}", note="example xai/collections/lifecycle.py remove document (also deletes the file!)")24st, _, _ = X("DELETE", f"/v1/collections/{cid}", note="example xai/collections/lifecycle.py delete collection"); print("delete collection:", st)25X("DELETE", f"/v1/files/{fid}", note="example xai/collections/lifecycle.py delete file (404 expected after remove)")26