Skip to main content
The official Pocketsflow SDK for Node.js provides a convenient way to interact with the Pocketsflow API from your server-side applications.

Installation

npm install pocketsflow

Quick Start

import { Pocketsflow } from 'pocketsflow';

const pf = new Pocketsflow({
  apiKey: 'pk_live_your_api_key_here',
});

// List all products
const products = await pf.products.list();
console.log(products);

Configuration

const pf = new Pocketsflow({
  apiKey: 'pk_live_your_api_key_here', // Required
  baseUrl: 'https://api.pocketsflow.com', // Optional, defaults to production
  timeout: 30000, // Optional, request timeout in milliseconds
});

Configuration Options

OptionTypeDefaultDescription
apiKeystringRequiredYour Pocketsflow API key
baseUrlstringhttps://api.pocketsflow.comAPI base URL
timeoutnumber30000Request timeout in ms

Products

Manage your digital products and subscriptions.

List Products

const products = await pf.products.list();

Get Product

const product = await pf.products.get('product_id');

Create Product

const product = await pf.products.create({
  name: 'Premium Course',        // Required
  price: 99.99,                  // Required
  description: 'Learn everything about...', // Optional
  subtitle: 'The complete guide',           // Optional
  published: true,               // Optional (default: true)
  slug: 'premium-course',        // Optional
  thumbnail: 'https://...',      // Optional
  images: ['https://...'],       // Optional
  showSales: true,               // Optional (default: true)
  showReviews: true,             // Optional (default: true)
  refundPolicy: '30-day money back guarantee', // Optional
  hasFirstName: true,            // Optional - collect first name
  hasLastName: true,             // Optional - collect last name
  payWant: false,                // Optional - pay what you want
  minPrice: 10,                  // Optional - for pay what you want
  maxPrice: 100,                 // Optional - for pay what you want
});
Required fields:
  • name - Product name
  • price - Product price

Update Product

const updated = await pf.products.update('product_id', {
  name: 'Updated Name',
  price: 149.99,
});

Delete Product

await pf.products.delete('product_id');

Product Variants

Create pricing tiers or versions of your products.

Create Variant

const variant = await pf.variants.create({
  productId: 'product_id',
  name: 'Premium Version',
  price: 149.99,
});

Delete Variant

await pf.variants.delete('variant_id');

Orders

Access and manage customer orders.

List Orders

const { orders, pagination } = await pf.orders.list({
  page: 1,
  pageSize: 20,
  startDate: '2024-01-01',
  endDate: '2024-12-31',
  productId: 'product_id', // optional filter
});

console.log(`Found ${pagination.totalCount} orders`);
console.log(`Page ${pagination.currentPage} of ${pagination.totalPages}`);

Get Order

const order = await pf.orders.get('order_id');

Customers

Access your customer base.

List Customers

const customers = await pf.customers.list({
  email: 'customer@example.com', // optional filter
  sortBy: 'createdAt',
  sortOrder: 'desc',
  limit: 100,
  offset: 0,
});

Get Customer

const customer = await pf.customers.get('customer_id');

Reviews

Access product reviews and ratings.

List Reviews

// All reviews
const reviews = await pf.reviews.list();

// Filter by product
const productReviews = await pf.reviews.list({
  productId: 'product_id',
});

Discounts

Create and manage discount codes.

List Discounts

const discounts = await pf.discounts.list();

Get Discount

const discount = await pf.discounts.get('discount_id');

Create Discount

const discount = await pf.discounts.create({
  name: 'Summer Sale',           // Required
  code: 'SUMMER20',              // Required - what customers enter
  mainProductIds: ['product_id_1', 'product_id_2'], // Required
  value: 20,                     // Required
  valueType: 'percentage',       // Optional (default: 'percentage') - or 'fixed'
  active: true,                  // Optional (default: true)
  expiration: '2024-12-31',      // Optional - ISO 8601 date
});
Required fields:
  • name - Discount name
  • code - Discount code customers enter at checkout
  • mainProductIds - Array of product IDs this discount applies to
  • value - Discount value (percentage or fixed amount)

Update Discount

const updated = await pf.discounts.update('discount_id', {
  value: 25,
  active: true,
});

Delete Discount

await pf.discounts.delete('discount_id');

Upsells

Configure post-purchase upsell offers.

List Upsells

const { upsells, salesWithUpsells } = await pf.upsells.list();

Get Upsell

const upsell = await pf.upsells.get('upsell_id');

Create Upsell

const upsell = await pf.upsells.create({
  mainProductIds: ['product_id'],      // Required - products that trigger this upsell
  upsellProductId: 'premium_product_id', // Required - product to upsell
  upsellPrice: 49.99,                  // Required - upsell price
  name: 'Premium Upgrade',             // Optional
  offer: 'Special one-time offer!',    // Optional - headline text
  upsellDescription: 'Upgrade to premium!', // Optional
  primaryButtonText: 'Yes, upgrade me!',    // Optional
  secondaryButtonText: 'No thanks',         // Optional
  active: true,                        // Optional (default: true)
});
Required fields:
  • mainProductIds - Array of product IDs that trigger this upsell
  • upsellProductId - The product ID to offer as an upsell
  • upsellPrice - Price of the upsell offer

Update Upsell

const updated = await pf.upsells.update('upsell_id', {
  upsellPrice: 39.99,
  active: true,
});

Delete Upsell

await pf.upsells.delete('upsell_id');

Subscriptions

Manage recurring subscriptions.

List Subscriptions

const subscriptions = await pf.subscriptions.list({
  page: 1,
  pageSize: 20,
  startDate: '2024-01-01',
  endDate: '2024-12-31',
});

Get Subscription

const subscription = await pf.subscriptions.get('subscription_id');

Cancel Subscription

await pf.subscriptions.cancel('subscription_id');

Refund Subscription

await pf.subscriptions.refund('subscription_id');

Webhooks

Configure webhook endpoints to receive real-time events.

List Webhooks

const webhooks = await pf.webhooks.list();

Get Webhook

const webhook = await pf.webhooks.get('webhook_id');

Create Webhook

const webhook = await pf.webhooks.create({
  url: 'https://your-server.com/webhook', // Required
  events: ['order.completed', 'order.refunded'], // Required
  description: 'My webhook endpoint',     // Optional
  active: true,                           // Optional (default: true)
});
Required fields:
  • url - Your webhook endpoint URL
  • events - Array of events to subscribe to
Available events:
  • order.completed - When an order is completed
  • order.refunded - When an order is refunded
  • product.created - When a product is created
  • product.updated - When a product is updated
  • product.deleted - When a product is deleted
  • review.created - When a review is submitted
  • customer.created - When a new customer is created
  • customer.subscription.updated - Subscription plan changed
  • customer.subscription.deleted - Subscription canceled
  • invoice.payment_succeeded - Subscription payment succeeded
  • invoice.payment_failed - Subscription payment failed
  • invoice.created - New invoice created
  • invoice.upcoming - Invoice about to be created
  • payment_intent.succeeded - Initial payment succeeded
  • payment_intent.payment_failed - Initial payment failed
  • customer.subscription.trial_will_end - Trial ending soon
  • customer.subscription.pause - Subscription paused
  • customer.subscription.resumed - Subscription resumed

Update Webhook

const updated = await pf.webhooks.update('webhook_id', {
  events: ['order.created', 'order.refunded', 'subscription.created'],
  active: true,
});

Test Webhook

await pf.webhooks.test('webhook_id');

Delete Webhook

await pf.webhooks.delete('webhook_id');

User / Account

Access and update your account information.

Get Current User

const user = await pf.users.me();

console.log(user.email);
console.log(user.subdomain); // Your store subdomain

Update User Settings

const updated = await pf.users.update({
  firstName: 'John',
  lastName: 'Doe',
  currency: 'USD',
  country: 'US',
});

Checkout Sessions

Create hosted checkout pages for your products.

Create Checkout Session

const session = await pf.checkout.create({
  productId: 'product_id',
  successUrl: 'https://your-site.com/success',
  cancelUrl: 'https://your-site.com/cancel',
  customerEmail: 'customer@example.com', // optional
  discountCode: 'SUMMER20', // optional
  metadata: { orderId: '12345' }, // optional
});

// Redirect customer to checkout
console.log(session.url);

Refunds

Process order refunds.

List Refunds

const refunds = await pf.refunds.list();

Get Refund

const refund = await pf.refunds.get('refund_id');

Create Refund

const refund = await pf.refunds.create({
  orderId: 'order_id',
  amount: 29.99, // optional, defaults to full refund
  reason: 'Customer requested refund',
});

Error Handling

The SDK throws PocketsflowError for API errors:
import { Pocketsflow, PocketsflowError } from 'pocketsflow';

try {
  const product = await pf.products.get('invalid_id');
} catch (error) {
  if (error instanceof PocketsflowError) {
    console.error('API Error:', error.message);
    console.error('Status:', error.status); // HTTP status code
    console.error('Code:', error.code); // Error code if available
  }
}

Common Error Codes

StatusDescription
400Bad request - Invalid parameters
401Unauthorized - Invalid API key
404Not found - Resource doesn’t exist
429Too many requests - Rate limited
500Server error

TypeScript Support

The SDK includes full TypeScript definitions:
import {
  Pocketsflow,
  Product,
  Order,
  Customer,
  CreateProductParams,
  PocketsflowError,
} from 'pocketsflow';

// All types are available
const params: CreateProductParams = {
  name: 'My Product',
  price: 29.99,
  currency: 'USD',
};

const product: Product = await pf.products.create(params);

Requirements

  • Node.js 16.0.0 or later
  • A Pocketsflow account with API keys

Support