Installation
composer require dsj/snelstartapi
Publish the config file (if needed):
php artisan vendor:publish --provider="Dsj\SnelstartApi\SnelstartApiServiceProvider"
Configuration
Edit .env with your SnelStart API credentials:
SNELSTART_API_KEY=your_api_key_here
SNELSTART_API_SECRET=your_api_secret_here
SNELSTART_API_URL=https://api.snelstart.nl/v1
First Use Case: Fetching a Customer
use Dsj\SnelstartApi\Facades\SnelstartApi;
$customer = SnelstartApi::customer()->find(123);
dd($customer); // Returns customer data
Key Facades
SnelstartApi::customer() → Customer operationsSnelstartApi::order() → Order operationsSnelstartApi::product() → Product operations// 1. Create order payload
$orderData = [
'customer_id' => 123,
'items' => [
['product_id' => 456, 'quantity' => 2],
],
];
// 2. Submit via API
$order = SnelstartApi::order()->create($orderData);
// 3. Handle response
if ($order->success()) {
// Process success
} else {
// Handle errors (e.g., $order->errors())
}
Laravel Events Trigger custom events post-API calls:
event(new OrderCreated($order));
API Rate Limiting Cache responses for non-critical data:
$cachedProduct = cache()->remember("snelstart_product_{$id}", now()->addHours(1), fn() =>
SnelstartApi::product()->find($id)
);
Form Requests
Validate API responses in FormRequest classes:
public function rules()
{
return [
'snelstart_order_id' => 'required|exists:snelstart_orders,id',
];
}
Queue Jobs Offload API calls to queues for async processing:
dispatch(new SyncSnelstartCustomer($customerId));
Authentication
SNELSTART_API_KEY and SNELSTART_API_SECRET are never committed to version control.env() helper to validate keys at runtime:
if (empty(env('SNELSTART_API_KEY'))) {
throw new \RuntimeException('SnelStart API keys are missing.');
}
Pagination
->paginate() if available or manually loop through ->all():
$customers = SnelstartApi::customer()->all();
foreach ($customers as $customer) { ... }
Error Handling
null or throw exceptions. Normalize responses:
$response = SnelstartApi::order()->find($id);
if (is_null($response)) {
// Handle missing resource
}
Webhooks
HandleIncomingWebhook trait or a dedicated route:
Route::post('/snelstart/webhook', [SnelstartWebhookController::class, 'handle']);
Enable Logging
Add to config/snelstartapi.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
API Response Inspection
Use dd() or dump() on raw responses:
$rawResponse = SnelstartApi::order()->raw()->find($id);
dd($rawResponse);
Testing Mock the API in PHPUnit:
$mock = Mockery::mock('overload', Dsj\SnelstartApi\SnelstartApi::class);
$mock->shouldReceive('get')->andReturn(['data' => 'test']);
Custom Endpoints Extend the base client:
use Dsj\SnelstartApi\SnelstartApi;
class CustomSnelstartApi extends SnelstartApi
{
public function customEndpoint($data)
{
return $this->post('/custom', $data);
}
}
Middleware Add middleware to API calls (e.g., for logging):
SnelstartApi::withMiddleware(function ($request) {
Log::info('SnelStart API call', ['data' => $request->data]);
});
Model Observers Sync SnelStart data with Eloquent models:
class CustomerObserver
{
public function saved(Customer $customer)
{
SnelstartApi::customer()->update($customer->id, $customer->toArray());
}
}
How can I help you explore Laravel packages today?