Wait for the answer
Get the answer back by waiting for it with ask() or the long poll, or by a callback when your code cannot wait.
The Quickstart asked about Kiwi Studio's invoice and got file back. Between the two, a person had to pick up their phone, and that can take ten seconds or a day. There are two ways for your code to get the answer back: it waits for it, or it gives Nibify an address to call when the answer exists.
Two ways
- Wait.
ask()holds the line until the request ends, or with curl you callGET /v1/requests/{id}/response?wait=30in a loop. Your code stays running, and there is nothing to host. - Callback. The request carries a
callbackUrl, and Nibify calls it with a signedPOSTwhen the request ends. Your code can stop in between, but it needs a publichttpsaddress that verifies the signature.
Both see the same outcomes. A request ends in one of four states: answered, expired (its own deadline passed), dismissed (the person declined it) or cancelled (you withdrew it). Until then it is pending, and it stays pending for as long as nobody answers: a request has no deadline unless you give it one.
Wait with the SDK
ask() sends the request and returns when it ends. It never throws on an outcome: expired, dismissed and cancelled are values of result.status, like answered.
Underneath, ask() holds a long poll of up to 30 seconds and opens the next one until the request ends. Waiting costs nothing against your rate limit.
Wait less than forever
Without options, ask() waits for as long as it takes. To wait less, pass timeout in milliseconds, or an AbortSignal:
timeout is not expired. It means your code stopped waiting; the request is still pending, the notification is still on the phone, and the person can still answer it. result.request.wait() picks the wait up again, and takes the same timeout and signal.
An aborted signal resolves the same way, with status: 'timeout', once the request exists. If it aborts while the request is still being sent, ask() rejects with the signal's reason.
Wait from somewhere else
The request lives on the server, not in your process. Keep its requestId, and another process can wait for it later:
getRequest() reads the request as it stands, without waiting. The request it returns is the same handle ask() gives you.
Give the person a deadline
When the answer is useless after a while, say so with expiresAt. Here research-agent asks about paywalled sources and gives the person an hour; if nobody answers, the request becomes expired and the agent goes on without them.
The expiry is checked once a minute, so expired can arrive up to a minute after expiresAt.
Wait with curl
POST /v1/requests answers 201 with a requestId right away. Then ask for the outcome, holding the connection for up to 30 seconds:
The call returns as soon as the request ends, or after wait seconds, always with 200. Read status:
pending: the 30 seconds passed and nobody has answered yet. Call it again.answered:responseholdsactionName,contextand when it was answered.expired,dismissed,cancelled: the request is over, andresponseisnull.
wait goes from 0 to 30; 0, the default, answers at once. A value outside that range is a 422, never a shorter wait. These calls are not counted against your rate limit, however long or often you hold them.
Get called back
When your code cannot stay running, give the request a callbackUrl. Nibify sends one signed POST there when the request ends: message.answered, message.expired, message.dismissed or message.cancelled.
With the SDK, timeout: 0 sends the request and returns at once with its requestId, without waiting. With curl, the 201 is all there is.
The address must be public https. Private networks, localhost and plain http are refused with 422 callback_url_invalid, at creation and again when Nibify connects. Redirects are not followed.
The callback carries the same answer as the long poll, wrapped in an event and signed with your project's signing secret. Verify it before you trust it: Webhooks and signatures shows how, and what happens when your address does not answer.
Which one
- A script or an agent that runs until it is done:
ask(). It is one line and there is nothing to host. - A job with a time budget:
ask()withtimeout, andrequest.wait()from the next run. - A workflow engine or a serverless function:
callbackUrl. Nothing holds a connection while the person thinks. - A backend that wants every outcome of every request: a webhook endpoint, set once in the dashboard rather than per request. See Webhooks and signatures.
You can use both on the same request: a callbackUrl does not stop the long poll from answering.