Development Guide for StateSet Platform

Overview

This guide will walk you through the process of setting up and developing with the StateSet Platform. StateSet provides powerful tools for managing returns and other e-commerce operations.

Prerequisites

  • Node.js (version 18.10.0 or higher)
  • A StateSet Cloud account

Step 1: Install StateSet Node SDK

First, install the StateSet Node SDK in your project:

npm install stateset-node

Step 2: Create a New Project on StateSet Cloud

  1. Sign up for a free account on StateSet Cloud.
  2. Once logged in, create a new project.

Step 3: Generate an API Key

  1. In your StateSet Cloud project, navigate to the API section.
  2. Create a new API key and save it securely. You’ll need this to authenticate your requests.

Step 4: Using the StateSet Node SDK

Here’s an example of how to use the StateSet Node SDK to fetch returns from the StateSet API:

import { NextRequest, NextResponse } from "next/server";
import Stateset from 'stateset-node';

export const config = {
  runtime: 'edge',
};

export default async function handler(req: NextRequest) {
  try {
    // Initialize the StateSet client
    const apiKey = process.env.STATESET_API_KEY;
    
    if (!apiKey) {
      throw new Error('STATESET_API_KEY is not set');
    }

    console.log('Initializing StateSet client with API key:', apiKey.substring(0, 5) + '...');

    const stateset = new Stateset({apiKey});
    
    // Fetch returns
    const response = await stateset.returns.list();
    
    console.log('Response received:', response);

    const returns = response.returns;

    // Return the fetched returns
    return NextResponse.json({ returns });
    
  } catch (error) {
    console.error('Error fetching returns:', error);
    
    if (error instanceof Error) {
      return NextResponse.json({ error: error.message }, { status: 500 });
    } else {
      return NextResponse.json({ error: 'An unknown error occurred' }, { status: 500 });
    }
  }
}

Best Practices

  1. Environment Variables: Always use environment variables to store sensitive information like API keys.
  2. Error Handling: Implement robust error handling to manage API request failures gracefully.
  3. Rate Limiting: Be aware of any rate limits on the StateSet API and implement appropriate throttling if necessary.
  4. Data Validation: Validate the data received from the API before using it in your application.

Additional Resources

Support

If you need assistance with the StateSet Node SDK or have any questions, please contact our support team at support@stateset.com.