# Search documents

`POST` `https://goblins-call-api.onrender.com/v1/knowledge/search`

Returns what an agent with these documents would be handed for a question, best match first.

Needs a [workspace API key](https://www.cracklehq.com/docs/authentication).

## Body

- `documentIds` (string[], required, up to 500): The documents to search. Only documents of your workspace that are `ready` are searched.
- `query` (string, required, 1–300 chars): The question, as a caller would ask it.

## Response

- `hits[].kind` (enum): `card` for a question and answer taken from a document, `section` for a passage of its text.
- `hits[].similarity` (number): How close the match is in meaning, from 0 to 1.

## Errors

- `400`: The body failed validation. `details` lists each field and what is wrong with it.

Every request can also answer `401`, `429` and `500`: see [Errors](https://www.cracklehq.com/docs/errors).

## Notes

- An empty `hits` means the agent would tell the caller it does not have that information.
- The figures in the sample are placeholders.

Request:

cURL:

```bash
curl -X POST "https://goblins-call-api.onrender.com/v1/knowledge/search" \
  -H "Authorization: Bearer $CRACKLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "documentIds": [
    "kd_4b1f0c9d2e7a6f53"
  ],
  "query": "How long does a refund take?"
}'
```

Node.js:

```js
const response = await fetch('https://goblins-call-api.onrender.com/v1/knowledge/search', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.CRACKLE_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    documentIds: ['kd_4b1f0c9d2e7a6f53'],
    query: 'How long does a refund take?',
  }),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
```

Python:

```python
import os
import requests

response = requests.post(
    "https://goblins-call-api.onrender.com/v1/knowledge/search",
    headers={"Authorization": f"Bearer {os.environ['CRACKLE_API_KEY']}"},
    json={
        "documentIds": ["kd_4b1f0c9d2e7a6f53"],
        "query": "How long does a refund take?",
    },
    timeout=30,
)
response.raise_for_status()
data = response.json()
```

Response · 200 OK:

```json
{
  "hits": [
    {
      "kind": "card",
      "question": "How long does a refund take?",
      "text": "A refund for a cancelled order is paid within seven working days to the card that was used.",
      "documentId": "kd_4b1f0c9d2e7a6f53",
      "document": "Refund policy",
      "similarity": 0.71
    }
  ],
  "ms": 180
}
```

Source: https://www.cracklehq.com/docs/reference/knowledge/search
