SDK Quickstart

Getting started with the @gwop/sdk TypeScript package

View as Markdown

Quick start

1

Install the SDK

$npm install @gwop/sdk

ESM only. For CommonJS projects, use await import("@gwop/sdk").

2

Create your first invoice

1import { Gwop } from "@gwop/sdk";
2
3const gwop = new Gwop({
4 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY,
5});
6
7const { result: invoice } = await gwop.invoices.create({
8 idempotencyKey: crypto.randomUUID(),
9 body: {
10 amountUsdc: 5_000_000, // $5.00 USDC (6 decimals)
11 description: "Starter plan — 300 credits",
12 },
13});
14
15console.log(invoice.publicInvoiceId); // inv_7dbeeaad...
16console.log(invoice.agentProtocol); // Machine-readable payment instructions
3

Authenticate an agent by wallet

1// 1. Create an auth challenge ($0.001 USDC dust payment)
2const { result: intent } = await gwop.authIntents.create({
3 idempotencyKey: crypto.randomUUID(),
4});
5
6// 2. Agent pays the challenge via x402 — proving wallet ownership
7
8// 3. Exchange for a JWT
9const { result: token } = await gwop.authIntents.exchange({
10 authIntentId: intent.authIntentId,
11 idempotencyKey: crypto.randomUUID(),
12});
13
14console.log(token.principal.sub); // "base:0x742d..." — the agent's identity
15console.log(token.account.isNewAccount); // true on first auth
4

Handle payment webhooks

1const gwop = new Gwop({
2 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY,
3 webhookSecret: process.env.GWOP_WEBHOOK_SECRET,
4});
5
6const event = await gwop.validateWebhook({ request });
7
8if (event.body.eventType === "invoice.paid") {
9 console.log(event.body.data.publicInvoiceId);
10 console.log(event.body.data.paymentChain); // "base" or "solana"
11}

Constructor

1import { Gwop } from "@gwop/sdk";
2
3const gwop = new Gwop({
4 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY,
5 webhookSecret: process.env.GWOP_WEBHOOK_SECRET, // Required for validateWebhook()
6});

Options

OptionTypeDescription
merchantApiKeystringYour sk_m_* merchant API key
webhookSecretstringYour whsec_* secret for webhook verification
serverURLstringOverride API base URL
retryConfigRetryConfigOverride retry behavior
timeoutMsnumberRequest timeout in milliseconds
debugLoggerLoggerPass console to enable debug logging

The SDK also reads from environment variables:

Env varMaps to
GWOP_MERCHANT_API_KEYmerchantApiKey
GWOP_DEBUG=trueEnables debug logging

Response shape

All methods return { headers, result }:

1const { headers, result } = await gwop.invoices.create({
2 idempotencyKey: crypto.randomUUID(),
3 body: { amountUsdc: 5_000_000 },
4});
5
6console.log(result.id); // Merchant UUID
7console.log(result.publicInvoiceId); // inv_* payer-facing ID

Methods

Invoices

MethodDescription
gwop.invoices.create()Create an invoice for agent payment
gwop.invoices.list()List invoices with pagination and status filter
gwop.invoices.get()Get the public invoice view (takes publicInvoiceId)
gwop.invoices.cancel()Cancel an open invoice (takes merchant UUID id)

Auth

MethodDescription
gwop.authIntents.create()Create a wallet auth challenge
gwop.authIntents.exchange()Exchange settled intent for JWT (402 if unpaid)
gwop.authSessions.get()Get session status
gwop.authSessions.revoke()Revoke a session (logout)
gwop.auth.getJwks()Fetch JWKS for local JWT verification

Webhooks

MethodDescription
gwop.validateWebhook()Verify HMAC signature + parse typed event

Error handling

1import * as errors from "@gwop/sdk/models/errors";
2
3try {
4 await gwop.invoices.create({ body: { amountUsdc: -1 } });
5} catch (error) {
6 if (error instanceof errors.ErrorResponse) {
7 error.data$.error.code; // "VALIDATION_ERROR"
8 error.data$.error.message; // Human-readable description
9 error.statusCode; // 400
10 }
11
12 if (error instanceof errors.RateLimitError) {
13 error.data$.error.details?.retryAfterSeconds; // When to retry
14 }
15}

See Error codes for the full reference.