Step 1 of the recommended flow for a LOCAL file when you (the calling agent) have shell/code-execution access (Bash, a script, curl, etc.): reserves a document record + a one-time signed upload URL, so the actual file bytes travel via a plain HTTP PUT from your shell — never through your own context/output tokens, regardless of file size.
Required: filename. Optional: mime_type, doc_type (default 'file'), title (default filename), description, amount, currency, status, occurred_at.
WHICH OF THE TWO WAYS: pass entity ('lead'|'contact'|'account'|'campaign') AND entity_id for a CRM document — an offer for a lead, an invoice for an account. Pass NEITHER for anything else: a screenshot for a SupportAIze ticket, a spreadsheet you are handing to another system, a file that has no CRM record. Both or neither; one alone is rejected, because a half-filed document appears in no list at all. Find the unfiled ones again with list_documents({unfiled:true}), and file one later with update_document({document_id, entity, entity_id}) once you know where it belongs.
THE STABLE REFERENCE IS THE document_id, not a URL. get_document_link mints a fresh signed URL on demand (default 1 h, max 24 h) and checks the tenant every time. So hand another system the document_id, or have it fetch the bytes once at attach time — never store the signed URL, it expires and then points at nothing while looking like a working attachment.
Optional provenance, for anything imported from somewhere else: source (e.g. 'beeper'), external_id (the id at that source) and source_meta (an object: network, chat_id, chat_title, sender, sender_id, sent_at, message_id — never credentials). Passing source + external_id also makes the call IDEMPOTENT: a second call for the same one returns the document already stored, with duplicate:true and NO upload_url, instead of a second record. That is the only place this can be enforced, because only the database sees both attempts.
Returns { document_id, upload_url, storage_path } — or { document_id, duplicate: true } when it was already stored. Next: from your shell, PUT the raw file to upload_url — e.g. `curl -T /path/to/file "$upload_url"`. No CallPilotAI credential is needed for that PUT; the URL is self-authorizing and expires in ~2 hours. Then call finalize_document_upload with document_id to confirm the upload landed, backfill its size/type, and run AI analysis. If you never complete the PUT, delete_document the reserved document_id to avoid leaving an empty record behind.