Keep your knowledge in sync

Upload documents, add questions and answers, resync your website and share it all with your agents, from your own systems, with a key that has `knowledge:write`.

Your agent is only as current as its knowledge. When a policy changes, a menu is reprinted or your website gets a new page, the agent should know straight away. This walkthrough sends that content to Careflow from your own systems, checks it has been processed, and confirms the agent can answer from it.

The example is a hotel that publishes a new room service menu, adds a question guests keep asking, and resyncs its website. You need an API key with Read & write on Knowledge. See API keys.

How knowledge is organised

Knowledge lives in sources. A source is an uploaded file, a website, or a shared collection of notes or questions and answers. Each source holds documents, and Careflow splits each document into passages your agents search during a conversation.

New content goes through processing before an agent can use it. Sources and documents have a status of processing, ready or failed, and agents only search documents that are ready. By default a source is shared with every agent in the workspace.

1. Upload a document

Send the file as multipart/form-data in a field called file. Careflow accepts .pdf, .docx, .txt, .md and .csv files, and judges the type by the file name's extension.

curl -X POST https://api.trycareflow.com/knowledge/documents \
  -H "Authorization: Bearer sk_live_xxx" \
  -F "file=@room-service-menu.pdf"
import { readFile } from "node:fs/promises";
 
const form = new FormData();
form.append("file", new Blob([await readFile("room-service-menu.pdf")]), "room-service-menu.pdf");
 
const res = await fetch("https://api.trycareflow.com/knowledge/documents", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.CAREFLOW_API_KEY}` },
  body: form,
});
const { data } = await res.json();
const documentId = data.document._id;

Don't set Content-Type yourself: fetch adds it with the right boundary. The response has message set to uploaded, the new document in data.document with status set to processing, and the source it created in data.source. Every upload creates a new source, so to replace a file, upload the new one and then delete the old source.

To make the file available to one agent only, add an assistantId field to the form with the agent's ID.

2. Wait until it's ready

Check the document every few seconds until processing finishes:

curl https://api.trycareflow.com/knowledge/documents/66f8d2b1c9e0a3f4b5c6d7a1 \
  -H "Authorization: Bearer sk_live_xxx"
async function waitForDocument(id) {
  for (;;) {
    const res = await fetch(`https://api.trycareflow.com/knowledge/documents/${id}`, {
      headers: { Authorization: `Bearer ${process.env.CAREFLOW_API_KEY}` },
    });
    const { data } = await res.json();
    if (data.document.status === "ready") return data.document;
    if (data.document.status === "failed") throw new Error(data.document.error);
    await new Promise((r) => setTimeout(r, 5000));
  }
}

ready means the agent can use it, and chunkCount says how many passages it became. failed comes with the reason in error. Once you've fixed the cause, POST /knowledge/documents/{id}/retry processes it again. While it works, progress gives a rough percentage.

3. Add a question and answer

Short facts don't need a file. Post a question and its answer as a qa source:

curl -X POST https://api.trycareflow.com/knowledge/sources \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "qa",
    "question": "Can I check in early?",
    "content": "Early check-in from 11am is possible when your room is ready."
  }'

Careflow keeps all your questions and answers together in one source called Q&A snippets, and adds each one as a document. The response has message set to created, with the source in data.source and the new document in data.document. Wait for the document as in step 2.

For a short note that isn't a question, send "type": "snippet" with a name and content. Notes are collected in a source called Notes.

To change an answer later, send the whole new text to the document. It replaces what was there, so include the question:

curl -X PATCH https://api.trycareflow.com/knowledge/documents/66f8d2b1c9e0a3f4b5c6d7b2 \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{"content": "Q: Can I check in early?\n\nA: Early check-in from 10am is possible when your room is ready."}'

The document goes back to processing and is ready again a few seconds later.

4. Add your website

A website source crawls pages from a starting address and keeps them as documents:

curl -X POST https://api.trycareflow.com/knowledge/sources \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "website",
    "sync": {
      "rootUrl": "https://www.harbourviewhotel.com",
      "maxDepth": 2,
      "maxPages": 50,
      "frequency": "weekly"
    }
  }'

rootUrl is required and must start with http:// or https://. maxDepth is how many links deep to follow, from 1 to 5 (2 if you leave it out). maxPages caps the crawl at up to 200 pages, and 0 lets Careflow decide. frequency is manual, daily or weekly: with daily or weekly, Careflow recrawls on its own. includePaths and excludePaths narrow the crawl to parts of the site.

The source comes back in data.source with status set to processing. Check it with GET /knowledge/sources/{id}. The crawl has finished when sync.lastRunAt appears, and the pages are searchable once status is ready. A failed source has the reason in error, and sync.lastError holds notes such as reaching the page limit. GET /knowledge/sources/{id}/documents lists the pages it found.

5. Resync after a change

When you publish new pages, ask for a fresh crawl rather than waiting for the schedule. Note the source's sync.lastRunAt first, then:

curl -X POST https://api.trycareflow.com/knowledge/sources/66f8d2b1c9e0a3f4b5c6d7c3/sync \
  -H "Authorization: Bearer sk_live_xxx"

The response has message set to queued. Check the source until sync.lastRunAt is newer than the value you noted and status is ready. Only website sources can be synced: other kinds return 400, and a source that's still processing returns 409. Files and questions are refreshed by uploading again or editing the document, as above.

6. Choose which agents use it

New sources are shared with every agent, so there's often nothing to do. To limit a source to particular agents, set its scope:

curl -X PATCH https://api.trycareflow.com/knowledge/sources/66f8d2b1c9e0a3f4b5c6d7c3 \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{"scope": "selected", "assistantIds": ["66f1c2a9e4b0f1a2b3c4d5e6"]}'

assistantIds replaces the whole list. To add or remove one agent without touching the rest, use POST or DELETE on /knowledge/sources/{id}/assistants/{assistantId}. GET /knowledge/sources?assistantId=... lists what one agent can use.

7. Check what the agent would say

Ask the agent's knowledge a question to confirm the new content is found:

curl -X POST https://api.trycareflow.com/assistants/66f1c2a9e4b0f1a2b3c4d5e6/knowledge/ask \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{"question": "Until what time is room service open?"}'

data.answer is the answer built from the agent's knowledge, and data.sources lists the documents it came from with a short excerpt of each. An empty answer means its knowledge doesn't cover the question yet. This is a POST, so it needs knowledge:write even though it changes nothing.

Where to go next