Before you start
Make an API key on the API keys page of your dashboard (see API keys) and put it in STACKGREP_KEY.
With curl
Shell
export STACKGREP_KEY=gw_...
# 1. a collection
curl -X POST https://stackgrep.com/api/collections \
-H "Authorization: Bearer $STACKGREP_KEY" \
-d '{"name": "invoices"}'
# {"id": 17}
# 2. documents: an id, the text, metadata to filter on
curl -X POST https://stackgrep.com/api/collections/invoices/docs \
-H "Authorization: Bearer $STACKGREP_KEY" \
-d '{"docs": [
{"id": "inv-1042", "text": "Invoice INV-1042\nTotal: 260 EUR\nTerms: net 30",
"meta": {"customer": "globex", "type": "invoice"}}
], "wait": true}'
# {"written": 1, "searchable": true}
# 3. search: exact text, or a regex with regex=1
curl -G https://stackgrep.com/api/collections/invoices/search \
-H "Authorization: Bearer $STACKGREP_KEY" \
--data-urlencode 'q=Total: [0-9]+ EUR' -d regex=1 -d filter=customer:globexThe answer:
Response
{"collection": "invoices", "took_ms": 3.1, "docs": 1,
"hits": [{"id": "inv-1042", "match": "Total: 260 EUR", "line": "Total: 260 EUR",
"meta": {"customer": "globex", "type": "invoice"}}]}"wait": true makes the documents searchable before the write is answered. Without it the write still waits until the documents are safely stored, and they're searchable everywhere within a few seconds.
In Python
Python (requests)
import os, requests
api = "https://stackgrep.com/api"
auth = {"Authorization": "Bearer " + os.environ["STACKGREP_KEY"]}
requests.post(f"{api}/collections", json={"name": "invoices"}, headers=auth)
requests.post(f"{api}/collections/invoices/docs", headers=auth, json={
"docs": [{"id": "inv-1042", "text": "Invoice INV-1042\nTotal: 260 EUR\nTerms: net 30",
"meta": {"customer": "globex"}}],
"wait": True,
})
r = requests.get(f"{api}/collections/invoices/search", headers=auth,
params={"q": "net 30", "filter": "customer:globex"})
for hit in r.json()["hits"]:
print(hit["id"], hit["line"])In TypeScript
TypeScript (fetch)
const api = "https://stackgrep.com/api"
const headers = { Authorization: "Bearer " + process.env.STACKGREP_KEY, "Content-Type": "application/json" }
await fetch(api + "/collections", { method: "POST", headers, body: JSON.stringify({ name: "invoices" }) })
await fetch(api + "/collections/invoices/docs", {
method: "POST", headers,
body: JSON.stringify({
docs: [{ id: "inv-1042", text: "Invoice INV-1042\nTotal: 260 EUR\nTerms: net 30", meta: { customer: "globex" } }],
wait: true,
}),
})
const q = new URLSearchParams({ q: "net 30", filter: "customer:globex" })
const { hits } = await (await fetch(api + "/collections/invoices/search?" + q, { headers })).json()
for (const h of hits) console.log(h.id, h.line)Next
- Connect your agent over MCP, so it can search, count and read these documents itself.
- Sync an S3 bucket instead of sending documents.
- Query syntax: regex, case and metadata filters.
Try it on your own documentsExact and regex search for your agents, from one API. Or see the engine on npm's source first.