DevelopersTools
@fianto/sdk
The server SDK - constructor options and env, resources, per-call options, the webhooks and handlers subpaths, errors and retries.
@fianto/sdk is the server side of every integration: a typed client for the v1 API, webhook
verification under @fianto/, and route handlers that work with any Request /
Response framework under @fianto/. The framework packages wrap those handlers.
pnpm add @fianto/sdkCreate the client
import { Fianto } from '@fianto/sdk';
// Reads FIANTO_APP_ID and FIANTO_APP_SECRET, and FIANTO_BASE_URL when it is set.
const fianto = new Fianto();
// Or pass everything yourself, for example where there is no process.env.
export function clientFor(env: { FIANTO_APP_ID: string; FIANTO_APP_SECRET: string }) {
return new Fianto({ appId: env.FIANTO_APP_ID, appSecret: env.FIANTO_APP_SECRET, maxRetries: 3 });
}
console.log(await fianto.application.retrieve());| Option | Env variable | Default | Notes |
|---|---|---|---|
appId | FIANTO_APP_ID | none, required | fian_app_… |
appSecret | FIANTO_APP_SECRET | none, required | fian_sk_live_… for mainnet, fian_sk_test_… for devnet. Server only. |
baseUrl | FIANTO_BASE_URL | the API of network, https:// by default | https, except localhost, 127.0.0.1 and [::1]. Without /v1: the SDK adds it. |
network | FIANTO_NETWORK | the key's network: 'devnet' for a fian_sk_test_ key, else 'mainnet' | 'mainnet' or 'devnet'. 'mainnet' takes only a live key; 'devnet' sends calls to https:// (exported as DEVNET_BASE_URL) and takes only a test key. A network that contradicts the key's prefix throws a FiantoError before any request. Any other value throws Invalid network: expected "mainnet" or "devnet". An explicit baseUrl wins; when you pass network to new Fianto() yourself, the other network's own host throws. |
timeoutMs | — | 30000 | Per attempt, 1 to 600000. |
maxRetries | — | 2 | Retries after the first attempt, 0 to 10. |
fetch | — | the global fetch | Your own fetch, for custom networking or tests. |
dangerouslyAllowBrowser | — | false | Without it, new Fianto() throws in a browser. |
A missing appId or appSecret throws at construction, naming the option and the variable to set.
Keys belong to the deployment that issued them: for a self-hosted or local backend, set
FIANTO_BASE_URL, or calls go to https:// and answer 401 invalid_.
A custom base URL like that is not checked against the key.
On the hosted APIs, a live key works only on mainnet and a test key only on devnet; the API refuses the
other with 401 api_. To use the devnet sandbox, use the application's test key: with
no network and no FIANTO_NETWORK, it selects devnet. A test key pointed at https://,
or a live key at https://, through baseUrl or FIANTO_BASE_URL, throws a
FiantoError before any request, however the network was set. These checks come with @fianto/sdk
0.6.0; an older release works with a test key if you set network: 'devnet'. See
Testing on devnet.
Never construct the client in a browser
The app secret would ship to every visitor. There is no publishable key: create checkout sessions on
your server and open them with @fianto/js. dangerouslyAllowBrowser: true
only turns off the check.
Resources
| Call | API route |
|---|---|
fianto. | GET /v1/application |
fianto. | POST /v1/ |
fianto. | GET /v1/ |
fianto. | POST /v1/ |
fianto. | POST /v1/ |
fianto., .list(params?) | GET /v1/orders/{id}, GET /v1/orders |
fianto. | GET /v1/ |
fianto., .list(params?) | GET /v1/payments/{id}, GET /v1/payments |
fianto., .list(params?) | GET /v1/, GET /v1/subscriptions |
fianto. | POST /v1/, at is now or period_end |
fianto., .list(params?) | GET /v1/products/{id}, GET /v1/products |
fianto., .list(params?) | GET /v1/prices/{id}, GET /v1/prices |
fianto., .list(params?) | GET /v1/events/{id}, GET /v1/events |
fianto. | POST /v1/ |
Ids that go into the path (every retrieve, cancel and reissueLink) are checked against
^[A-Za-z0-9_ before any request is sent. The orderId of retrieveByOrderId is your own
id, sent as a query parameter, and is not checked. Request and response
fields are the API's own, in snake_case: see Checkout sessions and Subscriptions.
list() returns a PagePromise: await it for one page ({ items, next_cursor }), or for await
it to walk every item. See Pagination.
Per-call options
Every method takes a last options argument:
| Option | What it does |
|---|---|
idempotencyKey | The Idempotency-Key for a POST. Default: a crypto.randomUUID() made once per call and reused on its retries. |
timeoutMs | Overrides the client's timeout for this call. |
signal | An AbortSignal that cancels the call. |
maxRetries | Overrides the client's retry count for this call. |
Every retrieve(id, …), checkoutSessions., reissueLink, application. and
sendTestEvent also take a params object that is empty today and sits before options, so pass
{} to reach it. subscriptions. is different: its params is the required { at }.
import { Fianto } from '@fianto/sdk';
const fianto = new Fianto();
export async function cancelCheckout(sessionId: string) {
// params is {} today; options comes after it.
return fianto.checkoutSessions.cancel(sessionId, {}, { idempotencyKey: `cancel:${sessionId}` });
}For crash-safe keys, see Idempotency.
Amounts
The API takes amounts as decimal strings ('12.50') and returns them as strings of base units of the record's
currency, with 6 decimals ('12500000'): USDC for crypto, US dollars for a card payment. usdc and usd convert
between the two without floating point, and money.format(base, currency) shows either:
import { usdc } from '@fianto/sdk';
usdc.toBaseUnits('12.5'); // '12500000'
usdc.fromBaseUnits('12500000'); // '12.5'
usdc.format('12500000'); // '12.50 USDC'
usdc.format('12500000', { symbol: false }); // '12.50'import { money, usd } from '@fianto/sdk';
usd.toBaseUnits('25.00'); // '25000000'
usd.format('25000000'); // '$25.00'
money.format('25000000', 'USD'); // '$25.00'
money.format('25000000', 'USDC'); // '25.00 USDC'usdc.toBaseUnits throws a UsdcError for more than 6 decimals, a sign, or anything that is not a number.
usd.* and money.format throw an AmountError (invalid_amount); UsdcError is its subclass.
Payment is CryptoPayment | CardPayment. Narrow on method before you read signature or card: a card payment
has currency: 'USD', signature, slot and block_time null, and customer.wallet null. See
Card payments.
The amount is what you receive
The amount you send is the price. When the service fee is above zero, it is added on top and the
payer pays it, and the payer also pays the network fee in USDC; total_amount in the API is
amount plus fee_amount plus network_fee_amount on checkout sessions, orders, payments and subscriptions.
Errors and retries
Every error the SDK throws extends FiantoError. An error response becomes an APIError with
status, code, message, field, details, requestId and headers, in one subclass per status:
| Class | Status |
|---|---|
AuthenticationError | 401 |
PermissionDeniedError | 403 |
InvalidRequestError | 400, 422 |
NotFoundError | 404 |
ConflictError | 409 |
RateLimitError | 429, with retryAfterSeconds |
ServiceUnavailableError | 503 |
InternalServerError | other 5xx |
Next to them: ConnectionError (no response arrived, or the API answered with a redirect, which
the SDK never follows), TimeoutError (over timeoutMs),
AbortError (your signal aborted) and UsdcError. After a network failure on a POST,
ConnectionError and TimeoutError carry the idempotencyKey to retry with.
The SDK retries network errors, timeouts, 408, 429, every 5xx, and a 409 only for
idempotency_ or checkout_. It never retries plan_limit_reached
(429) or subscriptions_ (503). It waits for Retry-After, or backs off
from 500 ms, doubling up to 8 s, with ±25 % jitter. A Retry-After over 10 seconds is not waited
on: the error is thrown at once.
Check errors with isFiantoError(err, ErrorCode.X) or isAPIError(err) rather than instanceof.
An app that loads the SDK both as ESM and as CommonJS has two copies of every class, and
instanceof a subclass fails across them. See Errors for the codes.
@fianto/ sdk/ webhooks
Verifies and builds webhook requests without a framework.
| Export | What it does |
|---|---|
verifyWebhook(rawBody, headers, { secret?, toleranceSeconds?, network? }) | Checks the signature and timestamp over the raw body and returns the typed event. secret is a string or an array during a roll, and defaults to FIANTO_. With network set, an event from the other network, or with no network, throws network_mismatch. |
WebhookVerificationError, isWebhookVerificationError | The error verifyWebhook throws, with a reason. |
WEBHOOK_EVENT_TYPES, isKnownEventType(type) | The event types this SDK version knows, and whether a type string is one of them. |
isEventType(event, type) | Narrows an event to one type. |
signWebhook({ event, secret, id?, timestamp? }) | Signs a body the way Fianto does and returns { body, headers }. |
sampleEvent(type, overrides?), sampleVerificationEvent() | A realistic fixture for each event type, and for the URL check. A sample carries network: 'mainnet' unless overrides sets network (also id, timestamp and data). method: 'card' builds the card form (US dollars, no fee, no signature, no wallet, a Visa ending 4242); payment.disputed is card only, and a type with no such form refuses it. |
refundDelta(data) | The signed move of one payment.refunded event, as a bigint: +applied_amount for succeeded and dispute_lost, -applied_amount for failed and reversed. Throws AmountError (invalid_amount) or RefundStatusError (unknown_). See Card payments. |
The tolerance is 300 seconds by default, from 1 to 3,600, and cannot be switched off. The reasons and the route code per framework are on Verify webhooks. In a test, sign a sample and post it to your own handler:
import { sampleEvent, signWebhook } from '@fianto/sdk/webhooks';
const { body, headers } = await signWebhook({ event: sampleEvent('order.paid'), secret: process.env.FIANTO_WEBHOOK_SECRET! });
await fetch('http://localhost:3000/api/webhooks/fianto', { method: 'POST', headers, body });@fianto/ sdk/ handlers
createWebhookHandler(options) and createCheckoutHandler(options) return a
function that takes a Request and resolves to a Response. Webhooks() / Checkout() in Next.js and
webhooks() / checkout() in Express and Hono are these two handlers, adapted to the framework.
Webhook handler options
| Option | Default | What it does |
|---|---|---|
secret | FIANTO_, read on each request | whsec_…, or an array during a roll. A given value is checked when the handler is created: an invalid one, [] included, throws there. |
toleranceSeconds | 300 | 1 to 3,600. |
network | not checked | 'mainnet' or 'devnet'. An event from the other network, or with no network, is refused after its signature verifies: 400 invalid_webhook, and onVerificationError gets the reason network_mismatch. |
maxBodyBytes | 1048576 (1 MiB) | A larger body is answered 413 {"error":"payload_. |
onCheckoutSessionCompleted … onTestEvent | — | One callback per event type: 21 in 0.7.0, 22 from 0.8.0, which adds onTransferRefundDue. A type your version has no callback for still reaches onEvent. |
onEvent | — | Runs after the type's own callback, for every verified event except the URL check. |
onVerificationError(error) | — | Why a request was refused. The response never says. |
onError(error, event) | — | A callback threw. The response is still 500, so Fianto retries. |
The handler answers endpoint. itself, 200 {"received": true} for a handled event,
400 {"error": "invalid_webhook"} for a failed check, 500 {"error": "handler_failed"} when a
callback throws, and 405 for anything but POST.
Checkout handler options
| Option | Default | What it does |
|---|---|---|
createSession(request, context) | required | Returns the session's params, decided on your server, or a Response to refuse. ui_mode is not accepted: the handler always uses popup. |
fianto | new Fianto() from the FIANTO_* variables, made on the first request | Pass a client to set credentials yourself. |
network | FIANTO_NETWORK, else the app secret's network | 'mainnet' or 'devnet', for the client the handler makes itself, as in new Fianto({ network }). Ignored when you pass fianto. |
allowedOrigins | the request URL's own origin | Origins allowed to call the route when the browser does not send Sec-Fetch-Site: same-origin. |
onError(error) | — | Receives the original error for every failure, including the ones the browser sees as 500. |
The handler accepts only POST (else 405 method_not_allowed), and only a same-origin request:
Sec-Fetch-Site: same-origin, or an Origin in allowedOrigins (else 403 forbidden_origin). It answers 200 { id, url }. When the order already has an open session with
the same terms, it reissues that session's link; with different terms it answers 409
order_ and leaves the open session alone.
Errors reach the browser as { error: { code, message } } only for an allowlist of codes, each with
the SDK's own message: payment_in_progress, order_already_paid, checkout_,
order_id_in_use, rate_limited, session_, plan_limit_reached,
order_, merchant_, payment_ (relayed as "Card payment
is not available for this checkout."), the 503 answers checkout_busy,
service_busy and subscriptions_ (relayed as 503, with Retry-After), and the validation
codes for your own params (validation_failed, url_insecure, amount_,
amount_out_of_range, amount_not_allowed, expires_, mode_not_supported,
price_id_required, price_not_found, price_ended, product_ended, price_not_recurring,
price_type_mismatch). Everything else, including a 401 or 403 from Fianto and any other 5xx,
answers 500 internal_error.
Wire onError
A wrong app secret or an unapproved account reaches the browser only as 500 internal_error.
Without onError, nothing on your side says why.
See also
Was this page helpful? Tell us
fianto.xyz