Bag Docs
SDKs

TypeScript SDK

Install, configure, and use the @getbagsapp/sdk package to integrate Bag.

TypeScript SDK

The official Bag SDK for TypeScript and JavaScript. It wraps the Bag REST API with typed methods, error handling, and a clean interface.

npm install @getbagsapp/sdk

Setup

import { Bag } from "@getbagsapp/sdk";

const bag = new Bag({
  apiKey: process.env.BAG_API_KEY!,
});

Configuration

OptionTypeRequiredDefaultDescription
apiKeystringYesYour Bag API key (bag_test_sk_* or bag_live_sk_*)
baseUrlstringNohttps://justusebag.xyzAPI base URL (override for self-hosted or local dev)

const link = await bag.paymentLinks.create({
  name: "Pro Plan",
  amount: 29.99,
  network: "base_sepolia",
});

Parameters:

FieldTypeRequiredDescription
namestringYesDisplay name
amountnumberYesPrice in USD
networkNetworkYesBlockchain network
descriptionstringNoDescription shown on checkout
currencystringNoDefaults to USD
tokenstringNoDefaults to USDC
networksNetwork[]NoMultiple chains
merchantWalletAddressstringNoOverride receiving wallet
merchantWalletAddressesRecord<string, string>NoPer-network wallets
const links = await bag.paymentLinks.list();
const link = await bag.paymentLinks.get("665f1a2b3c4d5e6f7a8b9c0d");
const updated = await bag.paymentLinks.update("665f1a2b3c4d5e6f7a8b9c0d", {
  amount: 39.99,
  active: false,
});
await bag.paymentLinks.delete("665f1a2b3c4d5e6f7a8b9c0d");

Transactions

List transactions

const transactions = await bag.transactions.list();

Create a transaction

const tx = await bag.transactions.create({
  amount: 29.99,
  token: "USDC",
  network: "base_sepolia",
  txHash: "0xabc123...",
  walletAddress: "0xCustomerWallet",
  customerEmail: "customer@example.com",
  paymentLinkId: "665f1a2b3c4d5e6f7a8b9c0d",
});

Parameters:

FieldTypeRequiredDescription
amountnumberYesPayment amount
tokenstringYesToken (e.g. USDC)
networkNetworkYesBlockchain network
txHashstringYesOn-chain transaction hash
walletAddressstringYesCustomer's wallet address
customerEmailstringNoCustomer email
customerNamestringNoCustomer name
customerAddressstringNoCustomer address
paymentLinkIdstringNoAssociated payment link

Checkout

Get a tax quote

const quote = await bag.checkout.getTaxQuote({
  paymentLinkId: "665f1a2b3c4d5e6f7a8b9c0d",
  customerAddress: {
    address_line_1: "100 Main St",
    address_city: "San Francisco",
    address_province: "CA",
    address_postal_code: "94105",
    address_country: "US",
  },
});

Returns: { subtotalCents, taxCents, totalCents, calculationId, quoteToken }

Create a checkout session

const session = await bag.checkout.createSession({
  linkId: "665f1a2b3c4d5e6f7a8b9c0d",
  quoteToken: quote.quoteToken,
  walletAddress: "0xCustomerWallet",
  walletType: "evm",
  network: "base_sepolia",
  customer: {
    name: "Jane Doe",
    email: "jane@example.com",
    address: "100 Main St, San Francisco, CA 94105",
    country: "US",
  },
  totalsSnapshot: {
    subtotalCents: quote.subtotalCents,
    taxCents: quote.taxCents,
    totalCents: quote.totalCents,
    calculationId: quote.calculationId,
  },
});

Get a checkout session

const status = await bag.checkout.getSession("session-uuid");

Submit a transaction hash

await bag.checkout.submit("session-uuid", "0xabc123...");

Error handling

The SDK throws BagError for API errors:

import { Bag, BagError } from "@getbagsapp/sdk";

try {
  await bag.paymentLinks.create({ name: "Test", amount: 0, network: "base" });
} catch (error) {
  if (error instanceof BagError) {
    console.error(`${error.statusCode}: ${error.message}`);
    console.error(`Code: ${error.code}`);
  }
}
PropertyTypeDescription
statusCodenumberHTTP status code
messagestringError message
codestring | undefinedMachine-readable error code

Types

Network

type Network =
  | "base" | "ethereum" | "polygon" | "solana"
  | "base_sepolia" | "eth_sepolia" | "solana_devnet";

TransactionStatus

type TransactionStatus =
  | "broadcasted" | "pending" | "confirming"
  | "completed" | "failed" | "refunded";

ApiResponse

interface ApiResponse<T> {
  status: "success" | "error";
  data?: T;
  message?: string;
  code?: string;
  hint?: string;
}

What's next

On this page