SDK Quickstart
Getting started with the @gwop/sdk TypeScript package
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
1 import { Gwop } from "@gwop/sdk"; 2 3 const gwop = new Gwop({ 4 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY, 5 }); 6 7 const { 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 15 console.log(invoice.publicInvoiceId); // inv_7dbeeaad... 16 console.log(invoice.agentProtocol); // Machine-readable payment instructions
3
Authenticate an agent by wallet
1 // 1. Create an auth challenge ($0.001 USDC dust payment) 2 const { 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 9 const { result: token } = await gwop.authIntents.exchange({ 10 authIntentId: intent.authIntentId, 11 idempotencyKey: crypto.randomUUID(), 12 }); 13 14 console.log(token.principal.sub); // "base:0x742d..." — the agent's identity 15 console.log(token.account.isNewAccount); // true on first auth
4
Handle payment webhooks
1 const gwop = new Gwop({ 2 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY, 3 webhookSecret: process.env.GWOP_WEBHOOK_SECRET, 4 }); 5 6 const event = await gwop.validateWebhook({ request }); 7 8 if (event.body.eventType === "invoice.paid") { 9 console.log(event.body.data.publicInvoiceId); 10 console.log(event.body.data.paymentChain); // "base" or "solana" 11 }
Constructor
1 import { Gwop } from "@gwop/sdk"; 2 3 const gwop = new Gwop({ 4 merchantApiKey: process.env.GWOP_MERCHANT_API_KEY, 5 webhookSecret: process.env.GWOP_WEBHOOK_SECRET, // Required for validateWebhook() 6 });
Options
| Option | Type | Description |
|---|---|---|
merchantApiKey | string | Your sk_m_* merchant API key |
webhookSecret | string | Your whsec_* secret for webhook verification |
serverURL | string | Override API base URL |
retryConfig | RetryConfig | Override retry behavior |
timeoutMs | number | Request timeout in milliseconds |
debugLogger | Logger | Pass console to enable debug logging |
The SDK also reads from environment variables:
| Env var | Maps to |
|---|---|
GWOP_MERCHANT_API_KEY | merchantApiKey |
GWOP_DEBUG=true | Enables debug logging |
Response shape
All methods return { headers, result }:
1 const { headers, result } = await gwop.invoices.create({ 2 idempotencyKey: crypto.randomUUID(), 3 body: { amountUsdc: 5_000_000 }, 4 }); 5 6 console.log(result.id); // Merchant UUID 7 console.log(result.publicInvoiceId); // inv_* payer-facing ID
Methods
Invoices
| Method | Description |
|---|---|
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
| Method | Description |
|---|---|
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
| Method | Description |
|---|---|
gwop.validateWebhook() | Verify HMAC signature + parse typed event |
Error handling
1 import * as errors from "@gwop/sdk/models/errors"; 2 3 try { 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.