API Integration Guide

Step-by-step guide to integrating the ShadowGrow API into your applications, websites, and services.

Frequently Asked Questions

How do I integrate the API into my application?

Get your API key from admin panel → API Manager, store it securely in environment variables, create an API client class to handle requests with proper authentication headers, implement error handling for API responses, and use the API methods to fetch products, create orders, manage users, and handle other operations.

How do I set up webhooks with the API?

Go to "Webhooks" in admin panel, create a new webhook, enter your webhook URL, select events to listen for (order.created, payment.completed, etc.), save webhook configuration. Your server will receive POST requests with event data when selected events occur.

How do I handle API errors in my integration?

Implement proper error handling: check for 401 status (invalid API key - handle authentication error), check for 429 status (rate limit exceeded - implement retry logic), check for other error statuses and handle appropriately. Always check response.status before processing data, and implement retry logic for transient errors.

What should I cache when integrating the API?

Cache product data to reduce API calls, implement cache invalidation on updates, use appropriate cache TTLs (time-to-live), cache user sessions and tokens, and cache static content like categories and tags. This improves performance and reduces API rate limit usage.

Integration Overview

What Can You Integrate?

The ShadowGrow API allows you to integrate:

  • Product catalogs into your website
  • Order processing and management
  • User authentication and profiles
  • File downloads and management
  • Payment processing
  • Inventory management
  • Custom dashboards and analytics

Integration Steps

Step 1: Get Your API Key

  1. Log in to the admin panel
  2. Navigate to "API Manager"
  3. Create a new API key with appropriate permissions
  4. Copy and securely store your API key

Step 2: Set Up Your Environment

Store your API key securely:

# .env file
SHADOWGROW_API_KEY=sk_live_your_api_key_here
SHADOWGROW_API_URL=https://your-domain.com/api/v1

Step 3: Create API Client

Example JavaScript API client:

class ShadowGrowAPI {
  constructor(apiKey, baseUrl) {
    this.apiKey = apiKey;
    this.baseUrl = baseUrl;
  }

  async request(endpoint, options = {}) {
    const url = `${this.baseUrl}${endpoint}`;
    const response = await fetch(url, {
      ...options,
      headers: {
        'Authorization': `Bearer ${this.apiKey}`,
        'Content-Type': 'application/json',
        ...options.headers
      }
    });

    if (!response.ok) {
      throw new Error(`API Error: ${response.status}`);
    }

    return await response.json();
  }

  // Product methods
  async getProducts(params = {}) {
    return this.request('/user/products', {
      method: 'GET'
    });
  }

  async getProduct(id) {
    return this.request(`/user/products/${id}`, {
      method: 'GET'
    });
  }

  // Order methods
  async createOrder(orderData) {
    return this.request('/user/orders', {
      method: 'POST',
      body: JSON.stringify(orderData)
    });
  }
}

// Usage
const api = new ShadowGrowAPI(
  process.env.SHADOWGROW_API_KEY,
  process.env.SHADOWGROW_API_URL
);

Common Integration Patterns

E-commerce Integration

Display products on your website:

// Fetch and display products
async function displayProducts() {
  const response = await api.getProducts({
    page: 1,
    per_page: 12,
    status: 'active'
  });

  const products = response.data;
  
  // Render products in your UI
  products.forEach(product => {
    // Create product card
    console.log(product.name, product.price);
  });
}

Order Processing

Create orders programmatically:

async function createOrder(cartItems, customerInfo) {
  const orderData = {
    items: cartItems.map(item => ({
      product_id: item.id,
      quantity: item.quantity,
      price: item.price
    })),
    customer: {
      name: customerInfo.name,
      email: customerInfo.email,
      address: customerInfo.address
    },
    payment_method: 'wallet'
  };

  const order = await api.createOrder(orderData);
  return order;
}

Webhooks

Setting Up Webhooks

Receive real-time notifications:

  1. Go to "Webhooks" in admin panel
  2. Create a new webhook
  3. Enter your webhook URL
  4. Select events to listen for (order.created, payment.completed, etc.)
  5. Save webhook configuration

Webhook Payload Example

{
  "event": "order.created",
  "timestamp": "2024-01-15T10:30:00Z",
  "data": {
    "order_id": 12345,
    "customer_email": "customer@example.com",
    "total": 99.99,
    "status": "pending"
  }
}

Best Practices

Error Handling

try {
  const products = await api.getProducts();
} catch (error) {
  if (error.status === 401) {
    // Handle authentication error
    console.error('Invalid API key');
  } else if (error.status === 429) {
    // Handle rate limit
    console.error('Rate limit exceeded');
  } else {
    // Handle other errors
    console.error('API Error:', error.message);
  }
}

Caching

  • Cache product data to reduce API calls
  • Implement cache invalidation on updates
  • Use appropriate cache TTLs
  • Cache user sessions and tokens

Related Guides