Skip to content
DocsWait for the answer

Theme

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 call GET /v1/requests/{id}/response?wait=30 in a loop. Your code stays running, and there is nothing to host.
  • Callback. The request carries a callbackUrl, and Nibify calls it with a signed POST when the request ends. Your code can stop in between, but it needs a public https address 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.

import { Nibify } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const result = await nibify.ask({
  sender: { name: 'invoice-flow' },
  notification: {
    title: 'Kiwi Studio invoice: $1,240, due Oct 30',
    body: 'Read from the PDF. File it as consulting, or fix it first.',
    quickActions: [
      { name: 'file', label: 'File it' },
      { name: 'fix', label: 'Fix it' },
    ],
  },
});

if (result.status === 'answered') {
  result.response.actionName; // 'file'
}

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:

ts
// sender and notification as in the Quickstart
const result = await nibify.ask({ sender, notification, timeout: 5 * 60 * 1000 });

if (result.status === 'timeout') {
  // Five minutes passed. The request is still pending on the phone.
  const later = await result.request.wait();
}

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:

ts
const { request } = await nibify.getRequest('msg_…');
const result = await request.wait();

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.

import { Button, Card, Column, Nibify, Row, Text } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const surface = {
  root: 'root',
  dataModel: {},
  components: [
    Card('root', { child: 'body' }),
    Column('body', { children: ['title', 'text', 'actions'] }),
    Text('title', { text: 'Include paywalled sources?', variant: 'h4' }),
    Text('text', { text: 'Three of the twelve sources need a login.' }),
    Row('actions', { justify: 'end', children: ['button-0', 'button-1'] }),
    Button('button-0', {
      variant: 'primary',
      child: 'button-0-label',
      action: { event: { name: 'include', context: {} } },
    }),
    Text('button-0-label', { text: 'Include them' }),
    Button('button-1', {
      variant: 'default',
      child: 'button-1-label',
      action: { event: { name: 'skip', context: {} } },
    }),
    Text('button-1-label', { text: 'Skip them' }),
  ],
};

const result = await nibify.ask(surface, {
  sender: { name: 'research-agent' },
  notification: {
    title: 'Include paywalled sources?',
    body: 'Three of the twelve sources need a login.',
    quickActions: [
      { name: 'include', label: 'Include them' },
      { name: 'skip', label: 'Skip them' },
    ],
  },
  expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
});

if (result.status === 'expired') {
  // nobody answered in time: go on without it
}

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:

bash
curl "https://api.nibify.app/v1/requests/msg_…/response?wait=30" \
  -H "Authorization: Bearer sk_test_…"

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: response holds actionName, context and when it was answered.
  • expired, dismissed, cancelled: the request is over, and response is null.

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.

import { Nibify } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const result = await nibify.ask({
  sender: { name: 'invoice-flow' },
  notification: {
    title: 'Kiwi Studio invoice: $1,240, due Oct 30',
    body: 'Read from the PDF. File it as consulting, or fix it first.',
    quickActions: [
      { name: 'file', label: 'File it' },
      { name: 'fix', label: 'Fix it' },
    ],
  },
  callbackUrl: 'https://invoice-flow.example.com/nibify/callback',
  timeout: 0,
});

result.requestId; // status 'timeout': sent, and the answer goes to callbackUrl

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() with timeout, and request.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.