@crablr/admin
v0.3.4
Published
Administrative API client for the Crablr platform, providing backend management capabilities and administrative operations.
Maintainers
Readme
@crablr/admin
Official Node.js SDK for the Crablr payment platform. Create payment intents and verify webhook signatures in your server-side applications.
Installation
npm install @crablr/adminQuick Start
import { createCrablrAdmin, createWebhook } from "@crablr/admin";
// Initialize the admin client
const crablr = createCrablrAdmin({
apiKey: "as_your_admin_secret_key_here",
});
// Create a payment intent
const paymentIntent = await crablr.createPaymentIntent({
price: { currency: "usd", amount: 29.99 }, // $29.99
metadata: { orderId: "order_123" }, // Optional
});
console.log(paymentIntent.id); // Payment intent ID
console.log(paymentIntent.clientSecret); // Secret for client
// Handle webhooks
const webhook = createWebhook({
webhookSecret: "whs_your_webhook_secret_here",
});
const paymentIntentId = webhook.assertSignature(
req.body, // Payload
req.headers["x-crablr-hmac-sha256"], // Signature
);API Reference
Admin Client
createCrablrAdmin(options)
Initialize an authenticated Crablr admin client.
const crablr = createCrablrAdmin({
apiKey: "as_your_admin_secret_key_here",
});Parameters:
apiKey(string | function) - Admin secret API key (must start withas_)
Returns: Admin client with methods: createPaymentIntent, getPaymentIntent, createCheckoutSession
Payment Intents
createPaymentIntent(input)
Create a new payment intent for accepting cryptocurrency payments.
const paymentIntent = await crablr.createPaymentIntent({
price: { currency: "usd", amount: 29.99 }, // $29.99 in USD
metadata: { orderId: "order_456" }, // Optional
});
console.log(paymentIntent.id); // Use this to track the payment
console.log(paymentIntent); // Send to client for payment executiongetPaymentIntent(input)
Retrieve a specific payment intent by ID.
const paymentIntent = await crablr.getPaymentIntent({
id: "507f1f77bcf86cd799439011",
});
console.log(paymentIntent.status);createCheckoutSession(input)
Create a hosted checkout session for streamlined payment flows.
const session = await crablr.createCheckoutSession({
lines: [
{
product: {
title: "Premium Widget",
subtitle: "Includes shipping",
},
unitPrice: { currency: "usd", amount: 19.99 }, // $19.99
quantity: 2,
},
],
successUrl: "https://yoursite.com/orders/success",
metadata: { orderId: "order_123" }, // Optional
blurb: "Order summary and details", // Optional
});
console.log(session.url); // Redirect user to this URLWebhook Handling
createWebhook(options)
Create a webhook handler for verifying webhook signatures.
const webhook = createWebhook({
webhookSecret: "whs_your_webhook_secret_here",
});webhook.assertSignature(payload, signature)
Verify webhook signature and validate payload expiration.
try {
const paymentIntentId = webhook.assertSignature(
req.body, // Payload
req.headers["x-crablr-hmac-sha256"], // Signature
);
console.log("Webhook verified for payment:", paymentIntentId);
} catch (error) {
console.error("Webhook verification failed:", error.message);
}Utilities
parseBody(reqBody)
Parse webhook request body (handles both string and object).
const { paymentIntentId, expiration, payload } = parseBody(req.body);parseHeaders(headers)
Extract webhook signature from request headers.
const signature = parseHeaders(req.headers); // Gets x-crablr-hmac-sha256parseReq(req)
Parse complete webhook request (body and headers).
const { body, receivedSignature } = parseReq(req);Configuration
Get your credentials from the Crablr dashboard:
- Admin Secret API Key: Starts with
as_- use for server-side operations - Webhook Secret: Starts with
whs_- configure in shop settings
Supported Blockchains
- Solana - SOL, 20,000+ SPL tokens
- Ethereum - ETH, ERC-20 tokens
- Polygon - MATIC, ERC-20 tokens
- Base - ETH, ERC-20 tokens
- Binance Smart Chain - BNB, BEP-20 tokens
Error Handling
All methods throw descriptive errors:
- Invalid API key: "Invalid admin secret API key. It must start with as_"
- Invalid webhook secret: "Invalid webhook secret. It must start with whs_"
- Expired signature: "Signature is expired."
- Invalid signature: "Invalid signature."
License
ISC
