Code Examples
Practical examples for integrating with Digisis APIs.
JavaScript/Node.js Examples
Initialize Client
javascript
import { DigisisClient } from '@digisis/sdk'
const client = new DigisisClient({
apiKey: process.env.DIGISIS_API_KEY,
environment: 'production' // or 'sandbox'
})Fetch Products
javascript
async function getProducts() {
try {
const response = await client.products.list({
limit: 10,
category: 'electronics'
})
console.log('Products:', response.data)
return response.data
} catch (error) {
console.error('Error fetching products:', error.message)
}
}Create Order
javascript
async function createOrder(customerId, items) {
try {
const order = await client.orders.create({
customer_id: customerId,
items: items,
shipping_address: {
street: '123 Main St',
city: 'Istanbul',
postal_code: '34000',
country: 'TR'
}
})
console.log('Order created:', order.id)
return order
} catch (error) {
console.error('Order creation failed:', error.message)
}
}Python Examples
Setup
python
pip install digisis-pythonBasic Usage
python
import digisis
client = digisis.Client(
api_key='your-api-key',
environment='production'
)
# List products
products = client.products.list(limit=10)
print(f"Found {len(products.data)} products")
# Get specific product
product = client.products.get('prod_123')
print(f"Product: {product.name} - ${product.price}")Error Handling
python
try:
order = client.orders.create({
'customer_id': 'cust_456',
'items': [{'product_id': 'prod_123', 'quantity': 2}]
})
except digisis.errors.InvalidRequestError as e:
print(f"Invalid request: {e.message}")
except digisis.errors.AuthenticationError as e:
print(f"Authentication failed: {e.message}")PHP Examples
Installation
bash
composer require digisis/digisis-phpBasic Usage
php
<?php
require_once 'vendor/autoload.php';
use Digisis\DigisisClient;
$client = new DigisisClient([
'api_key' => 'your-api-key',
'environment' => 'production'
]);
// List products
$products = $client->products->list(['limit' => 10]);
echo "Found " . count($products->data) . " products\n";
// Create order
$order = $client->orders->create([
'customer_id' => 'cust_456',
'items' => [
['product_id' => 'prod_123', 'quantity' => 2]
]
]);
echo "Order created: " . $order->id . "\n";
?>cURL Examples
List Products
bash
curl -X GET "https://api.digisis.com.tr/v1/products?limit=10" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json"Create Order
bash
curl -X POST "https://api.digisis.com.tr/v1/orders" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"customer_id": "cust_456",
"items": [
{
"product_id": "prod_123",
"quantity": 2
}
],
"shipping_address": {
"street": "123 Main St",
"city": "Istanbul",
"postal_code": "34000",
"country": "TR"
}
}'Webhook Examples
Express.js Webhook Handler
javascript
const express = require('express')
const app = express()
app.use(express.raw({ type: 'application/json' }))
app.post('/webhooks/digisis', (req, res) => {
const event = JSON.parse(req.body)
switch (event.type) {
case 'order.created':
console.log('New order:', event.data.id)
break
case 'order.fulfilled':
console.log('Order fulfilled:', event.data.id)
break
default:
console.log('Unhandled event:', event.type)
}
res.status(200).send('OK')
})
app.listen(3000, () => {
console.log('Webhook server running on port 3000')
})Testing
Unit Test Example (Jest)
javascript
describe('Digisis API Integration', () => {
let client
beforeEach(() => {
client = new DigisisClient({
apiKey: 'test-key',
environment: 'sandbox'
})
})
test('should fetch products', async () => {
const products = await client.products.list({ limit: 5 })
expect(products.data).toHaveLength(5)
expect(products.data[0]).toHaveProperty('id')
expect(products.data[0]).toHaveProperty('name')
})
test('should create order', async () => {
const order = await client.orders.create({
customer_id: 'test-customer',
items: [{ product_id: 'test-product', quantity: 1 }]
})
expect(order).toHaveProperty('id')
expect(order.status).toBe('pending')
})
})