This guide provides comprehensive instructions for integrating with the BrainSAIT Store API, including authentication, webhooks, and common integration patterns.
- Login to your admin panel at
https://store.brainsait.io/admin - Navigate to Settings → API Keys
- Generate a new API key with appropriate scopes
- Save your API key securely
# Test API access
curl -H "Authorization: Bearer YOUR_API_KEY" \
-H "X-Tenant-ID: your-tenant-id" \
https://brainsait-api-gateway.fadil.workers.dev/healthAPI_BASE_URL="https://brainsait-api-gateway.fadil.workers.dev"
API_KEY="your-production-api-key"
TENANT_ID="your-tenant-id"API_BASE_URL="http://localhost:8000"
API_KEY="your-development-api-key"
TENANT_ID="your-tenant-id"// JavaScript/Node.js example
const response = await fetch(`${API_BASE_URL}/api/v1/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Tenant-ID': 'your-tenant-id'
},
body: JSON.stringify({
email: 'user@example.com',
password: 'password'
})
});
const { access_token, refresh_token } = await response.json();// Include token in subsequent requests
const apiCall = await fetch(`${API_BASE_URL}/api/v1/store/products`, {
headers: {
'Authorization': `Bearer ${access_token}`,
'X-Tenant-ID': 'your-tenant-id'
}
});// Refresh expired token
const refreshResponse = await fetch(`${API_BASE_URL}/api/v1/auth/refresh`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Tenant-ID': 'your-tenant-id'
},
body: JSON.stringify({
refresh_token: refresh_token
})
});
const { access_token: new_token } = await refreshResponse.json();# Python example
import requests
headers = {
'Authorization': f'Bearer {API_KEY}',
'X-Tenant-ID': 'your-tenant-id',
'Content-Type': 'application/json'
}
response = requests.get(
f'{API_BASE_URL}/api/v1/analytics/overview',
headers=headers
)
data = response.json()async function getProducts(page = 1, limit = 20) {
const response = await fetch(
`${API_BASE_URL}/api/v1/store/products?page=${page}&limit=${limit}`,
{
headers: {
'Authorization': `Bearer ${token}`,
'X-Tenant-ID': tenantId
}
}
);
return await response.json();
}async function createProduct(productData) {
const response = await fetch(`${API_BASE_URL}/api/v1/store/products`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'X-Tenant-ID': tenantId,
'Content-Type': 'application/json'
},
body: JSON.stringify(productData)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
// Example usage
const newProduct = await createProduct({
name: {
en: "Digital Marketing Tool",
ar: "أداة التسويق الرقمي"
},
description: {
en: "Comprehensive digital marketing automation",
ar: "أتمتة شاملة للتسويق الرقمي"
},
price: 299.99,
currency: "USD",
category: "software",
tags: ["marketing", "automation", "digital"]
});async function createPaymentIntent(amount, currency = 'USD') {
const response = await fetch(
`${API_BASE_URL}/api/v1/payments/stripe/create-payment-intent`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'X-Tenant-ID': tenantId,
'Content-Type': 'application/json'
},
body: JSON.stringify({
amount: amount * 100, // Stripe expects cents
currency: currency.toLowerCase()
})
}
);
return await response.json();
}async function createPayPalOrder(items) {
const response = await fetch(
`${API_BASE_URL}/api/v1/payments/paypal/create-order`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'X-Tenant-ID': tenantId,
'Content-Type': 'application/json'
},
body: JSON.stringify({
items,
currency: 'USD'
})
}
);
return await response.json();
}# Python example for analytics
def get_analytics_overview(date_range=None):
params = {}
if date_range:
params['start_date'] = date_range['start']
params['end_date'] = date_range['end']
response = requests.get(
f'{API_BASE_URL}/api/v1/analytics/overview',
headers=headers,
params=params
)
return response.json()
# Usage
analytics = get_analytics_overview({
'start': '2024-01-01',
'end': '2024-01-31'
})- Navigate to Settings → Webhooks in admin panel
- Add your webhook endpoint URL
- Select events to subscribe to
- Configure webhook secret for verification
{
"events": [
"payment.completed",
"payment.failed",
"order.created",
"order.updated",
"user.created",
"tenant.updated"
]
}const express = require('express');
const crypto = require('crypto');
const app = express();
// Webhook secret from admin panel
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
// Middleware to verify webhook signature
function verifyWebhookSignature(req, res, next) {
const signature = req.headers['x-webhook-signature'];
const payload = JSON.stringify(req.body);
const expectedSignature = crypto
.createHmac('sha256', WEBHOOK_SECRET)
.update(payload)
.digest('hex');
if (signature !== `sha256=${expectedSignature}`) {
return res.status(401).send('Invalid signature');
}
next();
}
// Webhook endpoint
app.post('/webhook/brainsait', verifyWebhookSignature, (req, res) => {
const { event, data } = req.body;
switch (event) {
case 'payment.completed':
handlePaymentCompleted(data);
break;
case 'order.created':
handleOrderCreated(data);
break;
default:
console.log(`Unhandled event: ${event}`);
}
res.status(200).send('OK');
});
function handlePaymentCompleted(data) {
console.log('Payment completed:', data);
// Update local database, send notifications, etc.
}
function handleOrderCreated(data) {
console.log('Order created:', data);
// Process new order, update inventory, etc.
}from flask import Flask, request, abort
import hmac
import hashlib
import json
app = Flask(__name__)
WEBHOOK_SECRET = os.environ.get('WEBHOOK_SECRET')
def verify_signature(payload, signature):
expected_signature = hmac.new(
WEBHOOK_SECRET.encode(),
payload,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(f'sha256={expected_signature}', signature)
@app.route('/webhook/brainsait', methods=['POST'])
def handle_webhook():
signature = request.headers.get('X-Webhook-Signature')
payload = request.get_data()
if not verify_signature(payload, signature):
abort(401)
data = request.json
event = data.get('event')
if event == 'payment.completed':
handle_payment_completed(data['data'])
elif event == 'order.created':
handle_order_created(data['data'])
return 'OK', 200npm install @brainsait/store-sdkimport { BrainSAITStore } from '@brainsait/store-sdk';
const store = new BrainSAITStore({
apiKey: 'your-api-key',
tenantId: 'your-tenant-id',
baseURL: 'https://brainsait-api-gateway.fadil.workers.dev'
});
// Get products
const products = await store.products.list({
page: 1,
limit: 20,
category: 'software'
});
// Create order
const order = await store.orders.create({
items: [
{ productId: 'prod_123', quantity: 1 }
],
customerEmail: 'customer@example.com'
});
// Process payment
const payment = await store.payments.createStripeIntent({
amount: 299.99,
currency: 'USD',
orderId: order.id
});pip install brainsait-storefrom brainsait_store import BrainSAITStore
store = BrainSAITStore(
api_key='your-api-key',
tenant_id='your-tenant-id',
base_url='https://brainsait-api-gateway.fadil.workers.dev'
)
# Get analytics
analytics = store.analytics.get_overview(
start_date='2024-01-01',
end_date='2024-01-31'
)
# Create product
product = store.products.create({
'name': {'en': 'AI Tool', 'ar': 'أداة الذكاء الاصطناعي'},
'price': 199.99,
'category': 'ai-tools'
})
# Get orders
orders = store.orders.list(
status='completed',
limit=50
)<?php
// WooCommerce integration
class BrainSAIT_WooCommerce_Integration {
private $api_client;
public function __construct() {
$this->api_client = new BrainSAIT_API_Client(
get_option('brainsait_api_key'),
get_option('brainsait_tenant_id')
);
add_action('woocommerce_order_status_completed', [$this, 'sync_order']);
}
public function sync_order($order_id) {
$order = wc_get_order($order_id);
$this->api_client->orders->create([
'external_id' => $order_id,
'customer_email' => $order->get_billing_email(),
'total' => $order->get_total(),
'items' => $this->get_order_items($order)
]);
}
}// Salesforce integration example
class SalesforceBrainSAITSync {
constructor(salesforceClient, brainSAITClient) {
this.sf = salesforceClient;
this.bs = brainSAITClient;
}
async syncOpportunities() {
const opportunities = await this.sf.query(
"SELECT Id, Name, Amount, StageName FROM Opportunity WHERE StageName = 'Closed Won'"
);
for (const opp of opportunities.records) {
await this.bs.orders.create({
externalId: opp.Id,
amount: opp.Amount,
source: 'salesforce'
});
}
}
}{
"detail": "Authentication required",
"type": "authentication_error",
"code": "AUTH_001"
}async function apiCall(endpoint, options = {}) {
try {
const response = await fetch(endpoint, options);
if (!response.ok) {
const error = await response.json();
throw new Error(`API Error: ${error.detail} (${error.code})`);
}
return await response.json();
} catch (error) {
console.error('API call failed:', error);
// Implement retry logic for transient errors
if (error.code === 'RATE_LIMIT_EXCEEDED') {
await new Promise(resolve => setTimeout(resolve, 60000));
return apiCall(endpoint, options); // Retry after 1 minute
}
throw error;
}
}function handleRateLimit(response) {
const remaining = response.headers.get('X-Rate-Limit-Remaining');
const resetTime = response.headers.get('X-Rate-Limit-Reset');
console.log(`Requests remaining: ${remaining}`);
console.log(`Rate limit resets at: ${new Date(resetTime * 1000)}`);
}- Exponential Backoff: Implement exponential backoff for retries
- Request Queuing: Queue requests to avoid hitting limits
- Caching: Cache responses to reduce API calls
- Batch Operations: Use batch endpoints when available
// Jest test example
describe('BrainSAIT API Integration', () => {
test('should create and retrieve product', async () => {
const product = await store.products.create({
name: { en: 'Test Product' },
price: 99.99
});
expect(product.id).toBeDefined();
const retrieved = await store.products.get(product.id);
expect(retrieved.name.en).toBe('Test Product');
});
});// Mock server for development
const mockServer = require('json-server');
const server = mockServer.create();
server.get('/api/v1/store/products', (req, res) => {
res.json({
data: [
{ id: 1, name: { en: 'Mock Product' }, price: 99.99 }
],
pagination: { page: 1, total: 1 }
});
});
server.listen(3001);- Environment Variables: Store API keys in environment variables
- Key Rotation: Regularly rotate API keys
- Scope Limitation: Use minimal required scopes
- Audit Logging: Monitor API key usage
// Validate data before sending to API
function validateProductData(product) {
const required = ['name', 'price', 'category'];
for (const field of required) {
if (!product[field]) {
throw new Error(`Missing required field: ${field}`);
}
}
if (typeof product.price !== 'number' || product.price <= 0) {
throw new Error('Price must be a positive number');
}
}