PHP/Laravel Examples
Laravel Service Class
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class DemiService
{
protected string $apiKey;
protected string $baseUrl = 'https://api.getdemi.co/api/v1';
public function __construct()
{
$this->apiKey = config('services.demi.api_key');
}
protected function request(string $method, string $endpoint, array $data = [])
{
$response = Http::withHeaders([
'X-API-Key' => $this->apiKey,
])->$method($this->baseUrl . $endpoint, $data);
if ($response->failed()) {
throw new \Exception($response->json('error.message', 'API Error'));
}
return $response->json();
}
// Products
public function listProducts(int $limit = 20, int $offset = 0, ?string $search = null)
{
$params = compact('limit', 'offset');
if ($search) $params['search'] = $search;
return $this->request('get', '/products?' . http_build_query($params));
}
public function getProduct(int $id)
{
return $this->request('get', "/products/{$id}");
}
// Customers
public function listCustomers(int $limit = 20, int $offset = 0)
{
return $this->request('get', '/customers?' . http_build_query(compact('limit', 'offset')));
}
public function createCustomer(array $data)
{
return $this->request('post', '/customers', $data);
}
public function updateCustomer(int $id, array $data)
{
// Both PUT and PATCH work for partial updates
return $this->request('patch', "/customers/{$id}", $data);
}
public function deleteCustomer(int $id)
{
return $this->request('delete', "/customers/{$id}");
}
public function updateOrder(int $id, array $data)
{
return $this->request('patch', "/orders/{$id}", $data);
}
// Orders
public function listOrders(int $limit = 20, int $offset = 0, ?int $customerId = null)
{
$params = compact('limit', 'offset');
if ($customerId) $params['customerId'] = $customerId;
return $this->request('get', '/orders?' . http_build_query($params));
}
public function createOrder(array $data)
{
return $this->request('post', '/orders', $data);
}
public function cancelOrder(int $id)
{
return $this->request('delete', "/orders/{$id}");
}
}
Configuration
Add to config/services.php:
'demi' => [
'api_key' => env('DEMI_API_KEY'),
],
Add to .env:
DEMI_API_KEY=pk_your_api_key_here
Usage in Controller
<?php
namespace App\Http\Controllers;
use App\Services\DemiService;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function __construct(
protected DemiService $demi
) {}
public function index(Request $request)
{
$products = $this->demi->listProducts(
limit: $request->input('limit', 20),
offset: $request->input('offset', 0),
search: $request->input('search')
);
return view('products.index', compact('products'));
}
public function show(int $id)
{
$product = $this->demi->getProduct($id);
return view('products.show', compact('product'));
}
}
Create Customer Example
// paymentTerms options: 'Due on receipt' (default), 'Net 15', 'Net 30', 'Net 60'
$customer = $demi->createCustomer([
'name' => 'Acme Corp',
'email' => 'orders@acme.com',
'phone' => '+1-555-123-4567',
'contactName' => 'John Doe',
'paymentTerms' => 'Net 30',
]);
logger()->info("Created customer: {$customer['id']}");
Update Customer Example
// Partial update - only fields included are changed
$customer = $demi->updateCustomer(123, [
'email' => 'new-email@acme.com',
'phone' => null, // Set to null to clear field
]);
// Manage customer locations in one request
$customer = $demi->updateCustomer(123, [
'locations' => [
// Create new location (no id)
[
'name' => 'Downtown Office',
'address' => [
'address1' => '123 Main St',
'city' => 'Vancouver',
'countryDivision' => 'BC',
'country' => 'CA',
'postalCode' => 'V5K 0A1',
],
],
// Update existing location (with id)
['id' => 456, 'name' => 'Updated Location Name'],
// Delete location (with id and _delete flag)
['id' => 789, '_delete' => true],
],
]);
logger()->info("Customer now has " . count($customer['locations']) . " locations");
Create Order Example
$order = $demi->createOrder([
'customerId' => 123,
'deliveryDate' => '2024-01-15',
'poNumber' => 'PO-2024-001',
'items' => [
['productId' => 42, 'quantity' => 10],
['productId' => 43, 'quantity' => 5],
],
]);
logger()->info("Created order: {$order['id']}");
Error Handling
try {
$products = $demi->listProducts();
} catch (\Exception $e) {
logger()->error("Demi API Error: {$e->getMessage()}");
return back()->withErrors(['api' => 'Failed to fetch products']);
}