npm.io
1.1.0 • Published 3 weeks ago

@paykrypt/sdk

Licence
MIT
Version
1.1.0
Deps
0
Size
148 kB
Vulns
0
Weekly
0

@paykrypt/sdk

Official Node.js and TypeScript SDK for the PayKrypt crypto payment gateway API.

Installation

npm install @paykrypt/sdk
# or
pnpm add @paykrypt/sdk

Requires Node.js 18 or newer.

Quick Start

import { PayKryptClient } from '@paykrypt/sdk';

const paykrypt = new PayKryptClient({
  apiKey: process.env.PAYKRYPT_API_KEY!, // pk_<prefix>_<secret>
  baseUrl: process.env.PAYKRYPT_BASE_URL ?? 'https://api.paykrypt.io',
});

const intent = await paykrypt.paymentIntents.create({
  amount: '100.00',
  currency: 'USDT',
  description: 'Order #12345',
  customerEmail: 'customer@example.com',
  allowedChains: ['ethereum', 'tron'],
  expiresInMinutes: 60,
});

// Redirect the customer to the hosted checkout:
// https://gate.paykrypt.io/pay/<intent.id>

Configuration

const paykrypt = new PayKryptClient({
  apiKey: 'pk_12345678_...',
  baseUrl: 'https://api.paykrypt.io',
  timeout: 30_000,
  retries: 3,
});

All merchant API calls use:

Authorization: Bearer pk_<prefix>_<secret>

Sandbox and live environments are selected by baseUrl until separate key prefixes are introduced.

Idempotency

PayKrypt requires an Idempotency-Key on value-creating POST endpoints. The SDK generates one automatically for:

  • paymentIntents.create
  • payouts.create
  • payouts.createWithVerification
  • refunds.create
  • refunds.createWithVerification
  • conversions.convert

Pass your own stable key when retrying the same app-level action:

await paykrypt.paymentIntents.create(
  { amount: '100.00', currency: 'USDT' },
  { idempotencyKey: `order:${order.id}` },
);

Resources

await paykrypt.paymentIntents.retrieve('pi_...');
await paykrypt.paymentIntents.list({ page: 1, limit: 20 });
await paykrypt.paymentIntents.cancel('pi_...');

await paykrypt.payouts.create({
  amount: '95',
  currency: 'USDT',
  destinationAddress: 'TXYZ...',
  chainId: 'tron',
});
await paykrypt.payouts.stats();

await paykrypt.refunds.create({
  paymentIntentId: 'pi_...',
  amount: '50.00',
  reason: 'customer_request',
});

await paykrypt.webhooks.register({
  url: 'https://example.com/webhooks/paykrypt',
  events: ['payment.confirmed.v1'],
});

await paykrypt.addressBook.create({
  label: 'Treasury wallet',
  address: 'TXYZ...',
  chainId: 'tron',
});

await paykrypt.conversions.preview({
  fromAssetId: 1,
  toAssetId: 2,
  amount: 10,
});

await paykrypt.currencies.list();
await paykrypt.assets.list();
await paykrypt.assets.listByChain('tron');
await paykrypt.pricing.rates({ currency: 'USD', symbols: ['BTC', 'ETH', 'USDT'] });

Webhook Verification

PayKrypt signs webhook deliveries with X-PayKrypt-Signature and X-PayKrypt-Timestamp.

import express from 'express';
import { constructEvent } from '@paykrypt/sdk';

const app = express();

app.post('/webhooks/paykrypt', express.raw({ type: 'application/json' }), (req, res) => {
  const event = constructEvent(
    req.body,
    req.headers,
    process.env.PAYKRYPT_WEBHOOK_SECRET!,
  );

  if (event.type === 'payment.confirmed.v1') {
    // Fulfill the order.
  }

  res.json({ received: true });
});

The signature payload is:

<millisecond_timestamp>.<raw_body>

The SDK also accepts the older documented aliases Paykrypt-Signature and Paykrypt-Timestamp for compatibility.

Error Handling

import { PayKryptError } from '@paykrypt/sdk';

try {
  await paykrypt.paymentIntents.retrieve('pi_missing');
} catch (error) {
  if (error instanceof PayKryptError) {
    console.error(error.status, error.code, error.message);
  }
}

Publishing

This package publishes to npm from GitHub Releases. Set the repository secret NPM_TOKEN, bump package.json version, create a GitHub Release, and the publish workflow will run tests, build, dry-run packaging, and publish with npm provenance.