TypeScript
Install and use Nowgate's TypeScript SDK, for Node.js 22 and later, Deno, Bun and edge workers.
On this page
npm install nowgateThe SDK has no dependencies: it uses fetch and Web Crypto, so it runs on Node.js 22 and later,
Deno, Bun and edge workers. Its types come from the API's spec. It runs on servers only: an API
key must never reach a browser, so it refuses to start where a document exists.
Configure#
const nowgate = new Nowgate({ apiKey: env("NOWGATE_API_KEY"), baseUrl, fetch: counted });| Option | Default | What |
|---|---|---|
apiKey | (needed) | Your API key. Its environment picks the API. |
baseUrl | from the key | Another API, such as a local engine. |
timeout | 60000 | Milliseconds each attempt may take. |
maxRetries | 2 | How many times a failed attempt is retried. |
fetch | the global fetch | Your own HTTP client, with fetch's shape. |
appInfo | none | Your application, added to the User-Agent: my-shop/1.2. |
Every method takes a last options argument: { idempotencyKey, timeout, maxRetries, signal }.
Calls#
const payment = await nowgate.payments.create(
{ network: "tron", asset: "USDT", amount: "25.00", order_id: "1042" },
{ idempotencyKey: orderKey },
);A call's promise resolves to the data. Its withResponse() resolves to the data with the
status, the headers, the request's ID and the idempotency facts:
const again = await nowgate.payments
.create(
{ network: "tron", asset: "USDT", amount: "25.00", order_id: "1042" },
{ idempotencyKey: orderKey },
)
.withResponse();| Method | Operation |
|---|---|
nowgate.status.retrieve() | Service and network status |
nowgate.assets.list() | Supported networks and assets |
nowgate.rates.list() | List rates |
nowgate.rates.estimate() | Estimate a fiat price |
nowgate.rates.minAmount() | Get an asset's minimum |
nowgate.payments.create() | Create a payment |
nowgate.payments.retrieve() | Retrieve a payment |
nowgate.payments.list() and nowgate.payments.listAll() | List payments |
nowgate.paymentLinks.create() | Create a payment link |
nowgate.paymentLinks.retrieve() | Retrieve a payment link |
nowgate.paymentLinks.update() | Update a payment link |
nowgate.paymentLinks.list() and nowgate.paymentLinks.listAll() | List payment links |
nowgate.logos.create() | Upload a logo |
nowgate.balances.list() | List balances |
nowgate.payoutDestinations.create() | Add a payout destination |
nowgate.payoutDestinations.retrieve() | Retrieve a payout destination |
nowgate.payoutDestinations.disable() | Disable a payout destination |
nowgate.payoutDestinations.list() and nowgate.payoutDestinations.listAll() | List payout destinations |
nowgate.payouts.create() | Create a payout |
nowgate.payouts.retrieve() | Retrieve a payout |
nowgate.payouts.cancel() | Cancel a payout |
nowgate.payouts.list() and nowgate.payouts.listAll() | List payouts |
nowgate.payoutBatches.create() | Create a batch of payouts |
nowgate.payoutBatches.retrieve() | Retrieve a payout batch |
nowgate.payoutBatches.list() and nowgate.payoutBatches.listAll() | List payout batches |
nowgate.refunds.create() | Refund a payment |
nowgate.refunds.retrieve() | Retrieve a refund |
nowgate.refunds.cancel() | Cancel a refund |
nowgate.refunds.list() and nowgate.refunds.listAll() | List refunds |
nowgate.forwarding.retrieve() | Retrieve your forwarding rules |
nowgate.forwarding.update() | Set your forwarding rules |
nowgate.plans.create() | Create a plan |
nowgate.plans.retrieve() | Get a plan |
nowgate.plans.update() | Update a plan |
nowgate.plans.list() and nowgate.plans.listAll() | List plans |
nowgate.subscriptions.create() | Create a subscription |
nowgate.subscriptions.retrieve() | Get a subscription |
nowgate.subscriptions.update() | Update a subscription |
nowgate.subscriptions.cancel() | Cancel a subscription at once |
nowgate.subscriptions.rotateLink() | Replace the subscriber's link |
nowgate.subscriptions.list() and nowgate.subscriptions.listAll() | List subscriptions |
nowgate.invoices.retrieve() | Get an invoice |
nowgate.invoices.list() and nowgate.invoices.listAll() | List invoices |
nowgate.sandbox.simulatePayment() | Simulate a payer (sandbox) |
nowgate.sandbox.simulatePayout() | Fail a payout (sandbox) |
nowgate.sandbox.activatePayoutDestination() | Activate a destination now (sandbox) |
nowgate.sandbox.simulateSubscription() | Move a subscription's time (sandbox) |
nowgate.sandbox.simulateInvoice() | Pay an invoice (sandbox) |
Errors#
try {
await nowgate.payments.create({ network: "tron", asset: "USDT", amount: "25.0000001" });
} catch (err) {
if (!(err instanceof InvalidRequestError)) throw err;
// err.code is "amount_too_precise"; each field's problem has its JSON pointer
pointer = err.errors[0]?.pointer;
assert.equal(err.code, "amount_too_precise");
}The errors are NowgateError, ApiError and its classes by status (AuthenticationError,
PermissionError, NotFoundError, ConflictError, InvalidRequestError, RateLimitError,
ServerError), ConnectionError when there's no answer, and SignatureVerificationError. A
number where an amount goes is a TypeError, before anything is sent. See Errors.
Pages#
const all: string[] = [];
for await (const each of nowgate.payments.listAll()) all.push(each.id);Amounts#
Amounts are strings. Amounts.toUnits, Amounts.fromUnits and Amounts.compare convert and
compare them exactly, with bigint units. See Amounts.
Webhooks#
const chunks: Buffer[] = [];
for await (const chunk of request) chunks.push(chunk as Buffer);
const body = Buffer.concat(chunks); // the raw body, exactly as received
const signature = request.headers["nowgate-signature"];
const event = await Webhooks.constructEvent(body, signature, secret);Webhooks.verifySignature and Webhooks.constructEvent take the raw body as a string or bytes:
in Express, express.raw({ type: "application/json" }); in a Next.js route, await request.text().
See Webhooks.
Your own HTTP client#
Pass a function with fetch's shape as fetch. When there's no answer, it rejects, as fetch
does, and the client retries:
const counted = (url: string, init: RequestInit): Promise<Response> => {
requests++;
return fetch(url, init);
};