SDK in 5 minutes
Install an SDK, make a payment with pay(), handle a hold, and simulate in CI.
This quickstart takes an agent you run in your own code from nothing to a guarded payment in about five minutes. You install an SDK, call pay(), handle the three outcomes, and add a simulation to CI so a policy change can't silently block a normal purchase.
Install#
npm install @ndstill/sdkCreate an API key in the console under Developers, API keys. For this quickstart use a test key (nds_test_…): it runs the full decision pipeline and returns real traces, but never moves money. Give it the payments:write and budgets:read scopes, and scope it to one agent so the key can only ever pay as that agent.
export NDSTILL_API_KEY="nds_test_…"Make a payment#
One call. ndStill picks the rail the seller accepts and your policy allows, decides, and returns the payment.
import { NdStill } from "@ndstill/sdk";
const ndstill = new NdStill({ apiKey: process.env.NDSTILL_API_KEY });
const payment = await ndstill.pay({
agent: "agt_01JD8Q3W6Z",
amount: { amount: 4200, asset: "USD" }, // $42.00 in cents
counterparty: "Delta Air Lines",
purpose: "Flight SFO to JFK for the offsite",
});Money is always an integer in minor units plus an asset: 4200 USD is $42.00, and USDC uses six decimals (1000000 is 1 USDC). The SDKs send an Idempotency-Key for you, so a retried call never pays twice. With curl, send your own on every POST. The call maps to POST/payment-intents.
Handle the three outcomes#
Every payment comes back approved, held or declined. A hold is normal: it means the policy wants a person to look.
if (payment.status === "held") {
// A person approves first. Wait here, or listen for approval.decided.
await ndstill.approvals.wait(payment.approval_id, { timeoutMs: 10 * 60_000 });
} else if (payment.status === "declined") {
// Plain reasons, e.g. "Over Travel desk's weekly budget by $112.00"
console.log(payment.reasons.map((r) => r.message));
}Reasons are written for people and carry a machine code underneath, so your agent can act on them:
{
"code": "over_budget",
"stage": "budget",
"effect": "decline",
"message": "Over Travel desk's weekly budget by $112.00.",
"params": { "scope_name": "Travel desk", "window": "week", "over_by": { "amount": 11200, "asset": "USD" } }
}A good agent reacts to the code: on over_budget it stops and tells its user; on first_time_counterparty it waits for the approval; on agent_frozen it stops entirely. Every code is listed in Errors and decline reasons.
Waiting in-process is fine for scripts. For long-running services, subscribe to the approval.decided webhook instead and resume the task when it arrives. See Webhooks.
Simulate in CI#
Before you ship a policy change, check that ordinary purchases still go through. Simulation runs the same pipeline code with no reservation, no credential and no rail call, and returns would_outcome with the full trace.
// In CI, with a test key: fail the build if the policy would block a normal purchase.
const sim = await ndstill.simulate({
agent: "agt_01JD8Q3W6Z",
amount: { amount: 4200, asset: "USD" },
counterparty: "Delta Air Lines",
});
expect(sim.would_outcome).toBe("approve");
console.log(sim.trace.map((s) => `${s.stage}: ${s.result}`));The trace has one entry per stage, in order: authenticate, entitlement, freeze, funding, counterparty, mandate, policy, risk, budget, approval, record. The call maps to POST/decisions/simulate. Test mode, simulation and replay covers replaying real history against a draft policy.
Paying HTTP 402 challenges#
If your agent calls paid APIs that answer 402 Payment Required (x402 or MPP), the SDK can catch the challenge, ask ndStill for a decision, and only then let your wallet sign. Wrap your HTTP client once and every 402 goes through your policy. The x402 and USDC guide shows the hook in each language.