SDK Integration
The SpacePay SDK provides a simple way to integrate payments with minimal code and built-in functionality.
Installation
npm install spacepay-client-sdk
Requirements: Node.js 18+ or modern browsers with Fetch API support.
Basic Usage
import { SpacePayClient } from "spacepay-client-sdk";
// Initialize the client
const client = new SpacePayClient({
baseUrl: "https://api.spacepay.co.uk",
publicKey: "pk_test_your_public_key",
secretKey: "sk_test_your_secret_key",
});
// Create a payment
async function createPayment() {
try {
const payment = await client.createPayment({
orderId: "order_123",
amount: 100, // 100 cents = $1.00
currency: "USD",
redirectUrl: "https://merchant.example.com/checkout/success", // Optional
customMetadata: '{"cartId":"abc123","promo":"SUMMER24"}', // Optional
});
console.log("Payment URL:", payment.paymentUrl);
console.log("Payment ID:", payment.paymentId);
} catch (error) {
console.error("Error:", error.message);
}
}
// Check payment status
async function checkPaymentStatus(paymentId: string) {
try {
const status = await client.getPaymentStatus(paymentId);
console.log("Payment status:", status.status);
} catch (error) {
console.error("Error:", error.message);
}
}
SDK Features
SDK Benefits
- Lightweight: No heavy dependencies, minimal bundle size
- Type-safe: Full TypeScript support with comprehensive type definitions
- Universal: Works in Node.js 18+ and modern browsers
- Modern: Uses native fetch API and modern JavaScript features
- Robust: Built-in error handling, timeout management, and retry logic
Client Configuration
You can configure the client with additional options:
const client = new SpacePayClient({
baseUrl: "https://api.spacepay.co.uk",
publicKey: "pk_test_your_public_key",
secretKey: "sk_test_your_secret_key",
config: {
timeoutMs: 30000, // Request timeout in milliseconds (default: 30000)
},
});
Payment Status Values
The SDK returns payment statuses with the following values:
pending - Payment created, waiting for funds
processing - Payment detected, confirming on blockchain
completed - Payment confirmed and settled
failed - Payment failed or rejected
expired - Payment expired without completion
cancelled - Payment was cancelled
Error Handling
The SDK provides structured error handling:
import { ApiError } from "spacepay-client-sdk";
try {
const payment = await client.createPayment({
orderId: "order_123",
amount: 100,
currency: "USD",
});
} catch (error) {
if (error instanceof ApiError) {
console.error("API Error:", error.message);
console.error("Status:", error.status);
console.error("Request ID:", error.requestId);
} else {
console.error("Unexpected error:", error);
}
}
Next Steps