API · Connect your systems
Your endpoint & signatures
Hooks and tools reach your server the same way: an HTTPS POST with a JSON body, signed with the agent's secret. You answer with a JSON object. One endpoint can serve every hook and tool, or each can have its own.
The request
What arrives
POST /selvia/tools/check-order HTTP/1.1
Host: shop.example.com
Content-Type: application/json
User-Agent: SelviaAI-Tools/1.0
X-Selvia-Timestamp: 1795801594
X-Selvia-Signature: sha256=9c1f…a07e
X-Selvia-Delivery: c_3fa43d6961bebdc4
Authorization: Bearer YOUR_OWN_TOKEN
{"type": "tool", "name": "check_order", "tool": "check_order", "values": {"order_id": "A4512"},
"turn_id": 4, "session_id": "8d137734-f23f-4f1b-8480-0a57b8463ebb", "external_user_id": "u_88",
"agent_id": 7, "language": "en", "call_id": "c_3fa43d6961bebdc4", "sent_at": "2026-11-27T17:46:34Z"}| Header | Value |
|---|---|
X-Selvia-Timestamp | When we signed it, in Unix seconds. |
X-Selvia-Signature | sha256= + the HMAC-SHA256 of <timestamp>.<body> with the agent's signing secret, in hex. |
X-Selvia-Delivery | The call_id. The same on our one network retry of this call. |
User-Agent | SelviaAI-Tools/1.0 |
| Your headers | Whatever you set on the hook or tool, such as your own API key. |
| Body field | Hooks | Tools | Value |
|---|---|---|---|
type | ✓ | ✓ | hook or tool |
name | ✓ | ✓ | The hook's or tool's name. Serve several from one URL by switching on it. |
trigger | ✓ | call_start or pattern | |
match, text | pattern | What matched, and the visitor's whole turn as heard. | |
tool, values | ✓ | The tool's name, and the values the AI collected (shaped by its parameters). | |
turn_id | pattern | ✓ | Which turn of the call this came from. |
session_id | ✓ | ✓ | The session (as POST /v1/sessions returned it). Use it to send messages to this call. null on a Test call. |
external_user_id, external_session_id | ✓ | ✓ | Your own ids, when the session was started with them. |
agent_id, language | ✓ | ✓ | The agent, and the language of the call (e.g. en). |
call_id, sent_at | ✓ | ✓ | A unique id for this call to your server, and when we sent it. |
test | ✓ | ✓ | true only on a Test call. Answer it, but never carry out a real action. |
Your answer
Status 200, Content-Type: application/json, a JSON object of at most 64 KB. Every field is optional; answer {} when you have nothing to add.
| Field | Use |
|---|---|
data | Anything the AI should know: an object, a list, text. At most 8,000 characters of it reach the AI. For hooks, its scope decides how long it is kept; for tools, read Tools. |
messages | Up to 20 messages, as you would post them mid-call. Always honoured, for hooks and tools alike. |
Any of these counts as no answer:
- •No reply within the hook's or tool's timeout (connecting must take less than 5 s of it).
- •A status other than 2xx, including a redirect: redirects are not followed.
- •A body that isn't a JSON object, isn't sent as application/json, or is over 64 KB.
- •An address we can't use: not https, a certificate that doesn't check out, or a private network address.
On no answer, the AI is told your system didn't answer and not to guess; the avatar says it couldn't get it right now, and the call goes on. A connection error or a 502, 503 or 504 is retried once if time is left, with the same call_id.
Verify the signature
Each agent has one signing secret for its hooks and tools, whsec_…, shown once in the answer that creates its first tool or hook. Check every request with it before you trust it: compute the HMAC-SHA256 of the timestamp, a dot, and the raw body, compare it with X-Selvia-Signature in constant time, and refuse timestamps more than 5 minutes off.
- •Sign the raw body bytes, before any JSON parsing: re-serialised JSON won't match.
- •Don't allowlist our IP addresses: they change. The signature is the check.
- •Lost the secret, or think it leaked?
POST /v1/agent/{agent_id}/actions/signing-secretmakes a new one, shown once. Calls that start afterwards use it; calls already running keep signing with the old one until they end, so accept both for a while after you switch.GETon the same path shows whether there is one and when it was made.
| Secret | Signs or proves | Where you get it |
|---|---|---|
whsec_… | Our calls to your hooks and tools (this page). One per agent. | Shown with the agent's first tool or hook; renewed with …/actions/signing-secret |
whsec_… | Our data handoff and call-record webhooks. One per webhook destination. | The console, on the webhook destination |
vsk_… | Your signed visitor details to us (vars, your ids, start messages). | The console: the agent's “Pass data from your app or website” |
A complete example server
One call-start hook and two tools for the shoe shop on the overview: it verifies every request, answers the Test call without acting, checks the order belongs to the visitor, and makes the return safe to repeat.
# pip install fastapi uvicorn
import hashlib, hmac, json, os, time
from fastapi import FastAPI, Request, Response
app = FastAPI()
SECRET = os.environ["SELVIA_ACTIONS_SECRET"].encode() # whsec_… shown with the agent's first tool or hook
def verified(request: Request, body: bytes) -> bool:
ts = request.headers.get("X-Selvia-Timestamp", "")
sig = request.headers.get("X-Selvia-Signature", "")
expected = "sha256=" + hmac.new(SECRET, ts.encode() + b"." + body, hashlib.sha256).hexdigest()
fresh = ts.isdigit() and abs(time.time() - int(ts)) < 300
return fresh and hmac.compare_digest(sig, expected)
@app.post("/selvia/hooks/open-orders") # a call_start hook
async def open_orders(request: Request):
body = await request.body() # the raw bytes: the signature covers them exactly
if not verified(request, body):
return Response(status_code=401)
call = json.loads(body)
user = call.get("external_user_id") # the id YOU started the session with
if not user:
return {} # a visitor we don't know: nothing to add
orders = db.open_orders(user) # your own lookup
return {"data": {"open_orders": orders}} # the hook's scope decides how long the AI keeps it
@app.post("/selvia/tools/check-order") # a wait tool
async def check_order(request: Request):
body = await request.body()
if not verified(request, body):
return Response(status_code=401)
call = json.loads(body)
if call.get("test"): # the Test call: answer, but never act
return {"data": {"order_id": call["values"].get("order_id"), "status": "shipped (test)"}}
order = db.find_order(call["values"]["order_id"])
# The visitor SAID this number. Only answer for their own orders.
if not order or order.user_id != call.get("external_user_id"):
return {"data": {"found": False}}
return {"data": {"order_id": order.id, "status": order.status, "arrives": order.eta}}
@app.post("/selvia/tools/create-return") # a wait tool that changes something
async def create_return(request: Request):
body = await request.body()
if not verified(request, body):
return Response(status_code=401)
call = json.loads(body)
if call.get("test"):
return {"data": {"return_id": "R-TEST"}}
order = db.find_order(call["values"]["order_id"])
if not order or order.user_id != call.get("external_user_id"):
return {"data": {"done": False, "reason": "no such order for this customer"}}
# Safe to repeat: a second call for the same order returns the same return.
ret = db.find_return(order.id) or db.create_return(order.id)
return {"data": {"return_id": ret.id, "label": "sent by email"}}Test before you go live
/v1/agent/{agent_id}/tools/{name}/testAlso …/hooks/{name}/test, with {"text": "a sample visitor line"}. We call your endpoint once, from our server, signed exactly like a live call, with "test": true and session_id: null. At most 10 tests a minute per agent.
curl -X POST https://talk.selviaai.com/v1/agent/7/tools/check_order/test \
-H "Authorization: Bearer $SELVIA_API_KEY" \
-H "Content-Type: application/json" \
-d '{"values": {"order_id": "A4512"}}'Answer
{
"ok": true,
"status": 200,
"ms": 542,
"error": null,
"message": null,
"answer": { "data": { "order_id": "A4512", "status": "shipped (test)" } },
"notes": [],
"sent": { "type": "tool", "name": "check_order", "tool": "check_order", "values": { "order_id": "A4512" },
"session_id": null, "agent_id": 7, "language": "en", "call_id": "c_d4f96754febe9954",
"sent_at": "2026-11-27T17:36:32Z", "test": true }
}ok is what a live call would make of it; error and message say why not; notes lists anything a live call would skip or cut (a message that breaks a rule, data over 8,000 characters); sent is the exact body we posted.
Checklist
⚠ Make actions safe to repeat
call_id. Key every action on your own id and return the existing result the second time. X-Selvia-Delivery only spots our own network retry.- •Verify the signature and the timestamp on every request.
- •Answer the Test call (
"test": true) without doing anything real. - •Decide who the visitor is from ids you control (
external_user_idsent with your key or a signed token,session_id), never from what they say. - •Answer fast: under half a second for hooks, a second or two for wait tools.
- •Return only what the visitor may hear. The AI can repeat any of it.
- •Return 200 with a JSON object every time, {} when there is nothing to add.
- •Log the call_id with your own records, so you can match a call to a conversation later.