Skip to main content

Official SDKs

Node.js SDK

Installation

npm install @eventop/sdk
# or
yarn add @eventop/sdk

Quick Start

const { Eventop } = require('@eventop/sdk');

// Initialize client
const eventop = new Eventop({
  apiKey: process.env.EVENTOP_API_KEY
});

// Create checkout session
const session = await eventop.checkout.create({
  planId: 'premium-monthly',
  customerEmail: 'user@example.com',
  successUrl: 'https://yourdomain.com/success',
  cancelUrl: 'https://yourdomain.com/pricing'
});

console.log('Checkout URL:', session.url);

TypeScript Support

The SDK is written in TypeScript and includes full type definitions:
import { Eventop, CheckoutSession, Subscription } from '@eventop/sdk';

const eventop = new Eventop({
  apiKey: process.env.EVENTOP_API_KEY!
});

const session: CheckoutSession = await eventop.checkout.create({
  planId: 'premium-monthly',
  customerEmail: 'user@example.com',
  successUrl: 'https://yourdomain.com/success'
});

API Reference

Checkout

checkout.create(params)

Create a new checkout session.
const session = await eventop.checkout.create({
  planId: 'premium-monthly',
  customerEmail: 'user@example.com',
  customerId: 'user_123', // optional
  successUrl: 'https://yourdomain.com/success',
  cancelUrl: 'https://yourdomain.com/pricing', // optional
  metadata: { userId: '123' } // optional
});
Returns: Promise<CheckoutSession>

checkout.get(sessionId)

Retrieve a checkout session.
const session = await eventop.checkout.get('session_abc123');
Returns: Promise<CheckoutSession>

checkout.cancel(sessionId)

Cancel a pending checkout session.
await eventop.checkout.cancel('session_abc123');
Returns: Promise<void>

Subscriptions

subscriptions.list()

List all subscriptions for your merchant.
const subscriptions = await eventop.subscriptions.list();
Returns: Promise<Subscription[]>

subscriptions.get(subscriptionId)

Get details of a specific subscription.
const subscription = await eventop.subscriptions.get('ABC123...');
Returns: Promise<Subscription>

subscriptions.cancel(subscriptionId)

Cancel a subscription.
await eventop.subscriptions.cancel('ABC123...');
Returns: Promise<void>

Webhooks

webhooks.verifySignature(payload, signature, secret)

Verify a webhook signature.
const isValid = eventop.webhooks.verifySignature(
  req.body,
  req.headers['x-webhook-signature'],
  process.env.EVENTOP_WEBHOOK_SECRET
);
Returns: boolean

webhooks.constructEvent(payload, signature, secret)

Verify signature and parse webhook payload.
try {
  const event = eventop.webhooks.constructEvent(
    req.body,
    req.headers['x-webhook-signature'],
    process.env.EVENTOP_WEBHOOK_SECRET
  );
  
  console.log('Event:', event.event);
  console.log('Data:', event.data);
} catch (err) {
  console.error('Invalid signature');
}
Returns: WebhookPayload

Error Handling

The SDK throws typed errors for different failure scenarios:
const { Eventop, EventopError, AuthenticationError, InvalidRequestError } = require('@eventop/sdk');

try {
  const session = await eventop.checkout.create({ ... });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Invalid API key:', error.message);
  } else if (error instanceof InvalidRequestError) {
    console.error('Invalid request:', error.message);
  } else if (error instanceof EventopError) {
    console.error('API error:', error.message, error.statusCode);
  } else {
    console.error('Unexpected error:', error);
  }
}

Error Types

Error ClassDescription
AuthenticationErrorInvalid or expired API key
InvalidRequestErrorBad request parameters
NotFoundErrorResource not found
EventopErrorGeneral API error (base class)


Examples

Express.js

const express = require('express');
const { Eventop } = require('@eventop/sdk');

const app = express();
const eventop = new Eventop({
  apiKey: process.env.EVENTOP_API_KEY
});

app.post('/create-subscription', async (req, res) => {
  const { planId } = req.body;
  const user = req.user; // from your auth middleware
  
  try {
    const session = await eventop.checkout.create({
      planId,
      customerEmail: user.email,
      customerId: user.id.toString(),
      successUrl: `${process.env.BASE_URL}/success`,
      cancelUrl: `${process.env.BASE_URL}/pricing`
    });
    
    res.json({ checkoutUrl: session.url });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// Webhook endpoint
app.post('/webhooks/eventop', express.raw({ type: 'application/json' }), (req, res) => {
  const signature = req.headers['x-webhook-signature'];
  
  try {
    const event = eventop.webhooks.constructEvent(
      req.body,
      signature,
      process.env.EVENTOP_WEBHOOK_SECRET
    );
    
    switch (event.event) {
      case 'subscription.created':
        console.log('New subscription:', event.data);
        break;
      // Handle other events...
    }
    
    res.json({ received: true });
  } catch (err) {
    res.status(400).send('Webhook signature verification failed');
  }
});

Next.js API Routes

// pages/api/create-subscription.js
import { Eventop } from '@eventop/sdk';

const eventop = new Eventop({
  apiKey: process.env.EVENTOP_API_KEY
});

export default async function handler(req, res) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }
  
  const { planId, email } = req.body;
  
  try {
    const session = await eventop.checkout.create({
      planId,
      customerEmail: email,
      successUrl: `${process.env.NEXT_PUBLIC_URL}/success`,
      cancelUrl: `${process.env.NEXT_PUBLIC_URL}/pricing`
    });
    
    res.json({ checkoutUrl: session.url });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
}

Support