Vai al contenuto

SDK JavaScript e TypeScript

Installa @arbiro/sdk per Node 22+, runtime edge e browser. Client tipizzato, nuovi tentativi idempotenti, verifica dei webhook, importi e un pulsante React.

@arbiro/sdk è il client ufficiale. Non ha dipendenze a runtime: usa fetch e Web Crypto, quindi funziona su Node 22+, Deno, Bun, runtime edge e nel browser (nel browser vanno solo le funzioni di webhook e di importi; il client richiede una chiave segreta).

npm install @arbiro/sdk
pnpm add @arbiro/sdk

Client

import { Arbiro, ArbiroError } from "@arbiro/sdk";

const arbiro = new Arbiro({
  apiKey: process.env.ARBIRO_API_KEY!, // arb_test_... or arb_live_...
  // baseUrl: "https://arbiro.com", maxRetries: 2, timeoutMs: 30_000
});

const account = await arbiro.account.retrieve(); // { merchant_id, livemode }
Risorsa Metodi
paymentLinks create, list, retrieve
checkoutSessions create, retrieve(id, clientSecret)
payments list, retrieve, all (iteratore asincrono); addTracking non è ancora disponibile (beni fisici)
deliveries redeemToken
disputes list, retrieve, uploadEvidence, createEvidenceUpload, completeEvidenceUpload, listEvidence, appeal
webhookEndpoints create, list, disable, sendTest, deliveries
events list, retrieve, trigger (modalità test), deliveries, resend

Per Python, vedi l’SDK Python.

Nuovi tentativi e idempotenza

Ogni POST riceve un Idempotency-Key (un UUID casuale a meno che tu non passi idempotencyKey). Gli errori di rete e le risposte 429 e 5xx vengono ritentati con attesa usando la stessa chiave, quindi un nuovo tentativo non crea mai un secondo oggetto. Passa la tua chiave, come l’id dell’ordine, per rendere sicuri i nuovi tentativi anche dopo un riavvio del processo:

await arbiro.checkoutSessions.create({ amount: 25_000_000n, token: "USDC", category: "digital", success_url }, { idempotencyKey: "order_1042" });

Errori

Gli errori dell’API lanciano ArbiroError con status, code (stabile, leggibile da una macchina), message e requestId. I guasti di rete hanno status 0 e code network_error.

try {
  await arbiro.payments.retrieve(paymentId);
} catch (error) {
  if (error instanceof ArbiroError && error.status === 404) {
    // unknown payment for this key and mode
  }
}

Funzioni per gli importi

Gli importi sono bigint in unità atomiche, o stringhe di cifre nelle richieste. Non usare mai la virgola mobile per il denaro.

import { fromAtomic, toAtomic } from "@arbiro/sdk/money";

toAtomic("10.50"); // 10_500_000n
fromAtomic("50010000"); // "50.01"

Webhook

import { constructEvent, verifySignature } from "@arbiro/sdk/webhooks";

const event = await constructEvent(rawBody, request.headers.get("Arbiro-Signature"), secret);

constructEvent lancia WebhookSignatureError per una firma mancante, errata o scaduta e restituisce un evento tipizzato secondo event.type. Vedi Webhook.

React

import { ArbiroCheckoutButton } from "@arbiro/sdk/react";

<ArbiroCheckoutButton createSession={() => fetch("/api/checkout", { method: "POST" }).then((r) => r.json())} />;

Il pulsante reindirizza solo a https://arbiro.com/pay/... (o al checkoutOrigin che passi). Vedi Checkout ospitato.

Aggiornato il