Skip to main content

Overview

The Paystack class is the main entry point for the Paystack TypeScript SDK. It provides access to all Paystack APIs through module properties, each representing a different Paystack service.

Constructor

Paystack(secretKey)

Creates a new instance of the Paystack SDK.
secretKey
string
required
Your Paystack secret key. Must start with sk_live_ for production or sk_test_ for testing.
import { Paystack } from '@paystackly/core';

const paystack = new Paystack('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

Validation

The constructor validates the secret key format using Zod schema validation (from src/index.ts:34-42):
const secretKeySchema = z
  .string()
  .startsWith("sk_live_")
  .or(z.string().startsWith("sk_test_"));
if (!secretKeySchema.safeParse(secretKey).success) {
  throw new Error(
    "Invalid secret key. It must start with 'sk_live_' or 'sk_test_'.",
  );
}
The constructor will throw an error if the secret key doesn’t start with sk_live_ or sk_test_.

Properties

Once instantiated, the Paystack class provides access to the following module properties:

transaction

transaction
Transaction
Provides methods for interacting with Paystack’s Transaction API. Initialize, verify, list, and manage transactions.View Transaction API Reference →

transfer

transfer
Transfer
Provides methods for interacting with Paystack’s Transfer API. Initiate, finalize, and manage transfers to bank accounts.View Transfer API Reference →

recipient

recipient
Recipient
Provides methods for interacting with Paystack’s Transfer Recipient API. Create and manage transfer recipients.View Recipient API Reference →

split

split
Split
Provides methods for managing transaction splits on your integration. Create and manage split payment configurations.View Split API Reference →

virtualAccount

virtualAccount
VirtualAccount
Provides methods for interacting with Paystack’s Dedicated Virtual Account API. Create and manage dedicated virtual accounts.View Virtual Account API Reference →

verification

verification
Verification
Provides methods for verifying various Paystack entities. Resolve account numbers, validate accounts, and verify card BINs.View Verification API Reference →

miscellaneous

miscellaneous
Miscellaneous
Provides methods for accessing miscellaneous Paystack data. List supported banks, countries, and states.View Miscellaneous API Reference →

webhook

webhook
Webhook
Provides methods for verifying and processing Paystack webhooks. Handle incoming webhook events securely.View Webhook API Reference →

Usage Examples

Basic Initialization

From the SDK test suite (src/tests/sdk.test.ts:5-15):
import { Paystack } from '@paystackly/core';

const secret = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const paystack = new Paystack(secret);

// All modules are automatically initialized and available
console.log(paystack.miscellaneous);  // Miscellaneous instance
console.log(paystack.split);           // Split instance
console.log(paystack.transaction);     // Transaction instance
console.log(paystack.transfer);        // Transfer instance
console.log(paystack.verification);    // Verification instance
console.log(paystack.webhook);         // Webhook instance

Using Multiple Modules

import { Paystack } from '@paystackly/core';

const paystack = new Paystack('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

// Initialize a transaction
const transaction = await paystack.transaction.initialize({
  email: 'customer@email.com',
  amount: 10000, // Amount in kobo (100 NGN)
});

// List available banks
const banks = await paystack.miscellaneous.listBanks({
  country: 'nigeria',
});

// Verify an account number
const account = await paystack.verification.resolveAccount({
  account_number: '0123456789',
  bank_code: '058',
});

Error Handling

Invalid Secret Key

import { Paystack } from '@paystackly/core';

try {
  // This will throw an error
  const paystack = new Paystack('invalid_key');
} catch (error) {
  console.error(error.message);
  // "Invalid secret key. It must start with 'sk_live_' or 'sk_test_'."
}
All module properties are automatically instantiated when you create a Paystack instance. You don’t need to initialize them separately.

Module Initialization

Behind the scenes (from src/index.ts:43-51), the constructor initializes all modules with the secret key and base URL:
this.secretKey = secretKey;
this.transaction = new Transaction(this.secretKey, this.baseUrl);
this.split = new Split(this.secretKey, this.baseUrl);
this.virtualAccount = new VirtualAccount(this.secretKey, this.baseUrl);
this.webhook = new Webhook(this.secretKey, this.baseUrl);
this.verification = new Verification(this.secretKey, this.baseUrl);
this.miscellaneous = new Miscellaneous(this.secretKey, this.baseUrl);
this.transfer = new Transfer(this.secretKey, this.baseUrl);
this.recipient = new Recipient(this.secretKey, this.baseUrl);
All modules extend the Fetcher base class which handles HTTP requests to the Paystack API at https://api.paystack.co.