SDK Installation & Setup Guide

This comprehensive guide covers installing and configuring StateSet SDKs across different programming languages, package managers, and development environments.

Supported SDKs

StateSet provides official SDKs for the following languages:


Node.js SDK

Prerequisites

Runtime Requirements

  • Node.js: Version 16.0.0 or higher
  • npm: Version 7.0.0 or higher (or yarn/pnpm)
  • TypeScript: Version 4.5+ (optional but recommended)

Environment Setup

  • Modern terminal with UTF-8 support
  • Text editor with JavaScript/TypeScript support
  • Git for version control

Installation

# Install the main SDK
npm install stateset-node

# Install type definitions (if using TypeScript)
npm install --save-dev @types/node

# Install additional utilities (optional)
npm install dotenv winston

Quick Setup

Create a new project and configure the SDK:

# Create a new project
mkdir my-stateset-app
cd my-stateset-app

# Initialize package.json
npm init -y

# Install StateSet SDK
npm install stateset-node dotenv

# Create environment file
touch .env

Add your API credentials to .env:

# .env
STATESET_API_KEY=sk_live_51H9x2C2QlDjKpM2WYw5...
STATESET_ENVIRONMENT=production
STATESET_WEBHOOK_SECRET=whsec_3rK9pL7nQ2xS5mT8...

Basic Configuration

// app.js
import { StateSetClient } from 'stateset-node';
import dotenv from 'dotenv';

dotenv.config();

const client = new StateSetClient({
  apiKey: process.env.STATESET_API_KEY,
  environment: process.env.STATESET_ENVIRONMENT || 'sandbox',
  timeout: 30000,
  maxRetries: 3
});

// Test the connection
async function testConnection() {
  try {
    const health = await client.health.check();
    console.log('βœ… Connected to StateSet:', health.status);
  } catch (error) {
    console.error('❌ Connection failed:', error.message);
  }
}

testConnection();

Framework Integration

import express from 'express';
import { StateSetClient } from 'stateset-node';

const app = express();
const client = new StateSetClient({
  apiKey: process.env.STATESET_API_KEY
});

// Middleware to add StateSet client to request
app.use((req, res, next) => {
  req.stateset = client;
  next();
});

app.get('/orders', async (req, res) => {
  try {
    const orders = await req.stateset.orders.list();
    res.json(orders);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Python SDK

Prerequisites

Python Requirements

  • Python: Version 3.8 or higher
  • pip: Latest version recommended
  • Virtual environment: venv, conda, or poetry

Development Tools

  • Code editor with Python support
  • Git for version control
  • Terminal access

Installation

# Create virtual environment
python -m venv stateset-env
source stateset-env/bin/activate  # On Windows: stateset-env\Scripts\activate

# Install StateSet SDK
pip install stateset-python

# Install optional dependencies
pip install python-dotenv requests

Basic Configuration

# app.py
import os
from stateset import StateSetClient
from dotenv import load_dotenv

load_dotenv()

client = StateSetClient(
    api_key=os.getenv('STATESET_API_KEY'),
    environment=os.getenv('STATESET_ENVIRONMENT', 'sandbox'),
    timeout=30,
    max_retries=3
)

# Test the connection
def test_connection():
    try:
        health = client.health.check()
        print(f"βœ… Connected to StateSet: {health['status']}")
    except Exception as error:
        print(f"❌ Connection failed: {str(error)}")

if __name__ == "__main__":
    test_connection()

Framework Integration

from flask import Flask, jsonify, request
from stateset import StateSetClient
import os

app = Flask(__name__)
client = StateSetClient(api_key=os.getenv('STATESET_API_KEY'))

@app.route('/orders')
def get_orders():
    try:
        orders = client.orders.list()
        return jsonify(orders)
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)

Ruby SDK

Prerequisites

  • Ruby: Version 2.7 or higher
  • Bundler: For dependency management
  • RubyGems: Latest version

Installation

# Gemfile
gem 'stateset-ruby'
gem 'dotenv-rails' # For Rails apps
# or
gem 'dotenv' # For non-Rails apps
bundle install

Basic Configuration

# config/initializers/stateset.rb (Rails)
# or app.rb (Sinatra/plain Ruby)

require 'stateset'
require 'dotenv/load'

Stateset.configure do |config|
  config.api_key = ENV['STATESET_API_KEY']
  config.environment = ENV['STATESET_ENVIRONMENT'] || 'sandbox'
  config.timeout = 30
end

# Test connection
begin
  health = Stateset::Health.check
  puts "βœ… Connected to StateSet: #{health['status']}"
rescue => e
  puts "❌ Connection failed: #{e.message}"
end

PHP SDK

Prerequisites

  • PHP: Version 8.0 or higher
  • Composer: For dependency management
  • curl extension: Enabled

Installation

composer require stateset/stateset-php

Basic Configuration

<?php
require_once 'vendor/autoload.php';

use Stateset\StateSetClient;

$client = new StateSetClient([
    'api_key' => $_ENV['STATESET_API_KEY'],
    'environment' => $_ENV['STATESET_ENVIRONMENT'] ?? 'sandbox',
    'timeout' => 30
]);

// Test connection
try {
    $health = $client->health->check();
    echo "βœ… Connected to StateSet: " . $health['status'] . "\n";
} catch (Exception $e) {
    echo "❌ Connection failed: " . $e->getMessage() . "\n";
}
?>

Environment Configuration

Development Environment

# .env.development
STATESET_API_KEY=sk_test_51H9x2C2QlDjKpM2WYw5...
STATESET_ENVIRONMENT=sandbox
STATESET_WEBHOOK_SECRET=whsec_test_3rK9pL7nQ2xS5mT8...
LOG_LEVEL=debug

Production Environment

# .env.production
STATESET_API_KEY=sk_live_51H9x2C2QlDjKpM2WYw5...
STATESET_ENVIRONMENT=production
STATESET_WEBHOOK_SECRET=whsec_prod_3rK9pL7nQ2xS5mT8...
LOG_LEVEL=info

Docker Configuration

# Dockerfile
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
# docker-compose.yml
version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - STATESET_API_KEY=${STATESET_API_KEY}
      - STATESET_ENVIRONMENT=production
    volumes:
      - .:/app
      - /app/node_modules

Troubleshooting

Common Issues

Debug Mode

Enable debug logging to troubleshoot issues:

const client = new StateSetClient({
  apiKey: process.env.STATESET_API_KEY,
  debug: true,
  logLevel: 'debug'
});

Health Check Script

Create a health check script to verify your setup:

// health-check.js
import { StateSetClient } from 'stateset-node';

const client = new StateSetClient({
  apiKey: process.env.STATESET_API_KEY
});

async function runHealthCheck() {
  const checks = [
    { name: 'API Connection', test: () => client.health.check() },
    { name: 'Orders API', test: () => client.orders.list({ limit: 1 }) },
    { name: 'Customers API', test: () => client.customers.list({ limit: 1 }) }
  ];

  for (const check of checks) {
    try {
      await check.test();
      console.log(`βœ… ${check.name}: OK`);
    } catch (error) {
      console.error(`❌ ${check.name}: ${error.message}`);
    }
  }
}

runHealthCheck();

Next Steps

After installing the SDK:

  1. Follow a Quickstart Guide: Choose a quickstart guide based on your use case
  2. Explore API Reference: Review the API documentation for detailed endpoint information
  3. Set Up Webhooks: Configure webhook security for real-time updates
  4. Implement Error Handling: Follow error handling best practices

Support

If you encounter issues during installation: