---
docId: doc_95f545b7448b4f389eb2937f7d124353
title: "Upload a file"
description: "Add a document to the library, wait until it is processed, give it to an agent and ask about it."
sidebarTitle: "Upload a file"
---

# Upload a file

Upload a document and connect it to an agent to ask a question. The SDK uploads the bytes and waits for processing.

## Before you start

- A Platica API key in `PLATIC_API_KEY`.
- Optional: the path of a file, such as `./pricing.pdf`. Without one the script adds a short text.
- Node.js 20.3 or newer and `npm install platic-sdk`.

<span id="what-it-does"></span>

## Upload and process the document

- Adds the file to the library from the path you provide. If you pass no path, it uses a short text. Up to 1 MiB is sent in the same request; for files larger than 1 MiB and up to 50 MiB, the SDK opens a session and uploads the bytes.
- Waits for the processing job with `files.waitForJob`. A failed job is returned (`status: "failed"`), not thrown.
- Creates an agent, connects the file to it and asks it to summarize the file.
- Deletes the agent and the file at the end. `files.delete` asks for the `confirmationToken` for you.

## The code

```ts
import { readFile } from "node:fs/promises";
import { basename } from "node:path";
import { Platic } from "platic-sdk";

const apiKey = process.env.PLATIC_API_KEY;
if (!apiKey) {
  console.error("Set PLATIC_API_KEY to a Platica API key (pl_key_…).");
  process.exit(1);
}

const platic = new Platic({ apiKey });
const path = process.argv[2];

// 1. Create the file. The API accepts it at once and processes it in a job.
const accepted = path
  ? await platic.files.createFile({ data: await readFile(path), fileName: basename(path) })
  : await platic.files.createText({
      title: "Acme plans",
      text: "# Acme plans\n\n- Basic: $10 a month, up to 5 people.\n- Team: $50 a month, up to 30 people.\n- Enterprise: priced on request.",
    });
console.log(`file ${accepted.fileId}, job ${accepted.uploadId}`);

let agentId: string | undefined;
try {
  // 2. Wait for the job: parsing, chunking and embedding. A failed job is returned, not thrown.
  const job = await platic.files.waitForJob(accepted.fileId, accepted.uploadId, { timeoutMs: 5 * 60_000 });
  if (job.status !== "ready") throw new Error(`the file could not be processed: ${job.error?.code ?? job.status}`);
  console.log("file ready");

  // 3. Give it to an agent, and ask about it.
  const agent = await platic.agents.create({ name: "Library example", instructions: "Answer only from your files. Be brief." });
  agentId = agent.id;
  const link = await platic.agents.files.connect(agent.id, accepted.fileId);
  console.log(`agent ${agent.id} uses the file (${link.state})`);

  const turn = await platic.chat.send({ agentId: agent.id, message: "Summarize what your file says in two sentences." });
  for (const reply of turn.messages) console.log(`agent: ${typeof reply.content === "string" ? reply.content : JSON.stringify(reply.content)}`);
} finally {
  if (agentId) await platic.agents.delete(agentId);
  // `delete` confirms the agents that still use the file for you.
  await platic.files.delete(accepted.fileId);
  console.log("removed the example's agent and file");
}
```

## Run it

Save the code in `upload-a-file.ts` and run it with [tsx](https://tsx.is).

Set your API key and run the script:

```bash
export PLATIC_API_KEY=pl_key_...
npx tsx upload-a-file.ts
```

To upload a file of your own, pass its path:

```bash
npx tsx upload-a-file.ts ./pricing.pdf
```

## What to expect

```text
file file_…, job …
file ready
agent ag_… uses the file (…)
agent: <a two-sentence summary of the file>
removed the example's agent and file
```

If processing fails, the script shows the job's error code and removes the agent and file. If the wait expires, you receive `FileJobTimeoutError`; processing continues.

## API reference used

- [Create a file](/api-reference/files/create)
- [Upload a large file](/api-reference/files/upload)
- [File processing](/api-reference/files/processing)
- [Connect a file to an agent](/api-reference/agents/files/connect)
- [Send a chat message](/api-reference/chat/send)
- [Delete a file](/api-reference/files/delete)

## Next steps

- [All SDK examples](/sdk/examples/quickstart): the next one is [WhatsApp templates](/sdk/examples/whatsapp-templates).

- [Errors](/sdk/errors)
