Installation Add the package via Composer:
composer require milestone/leapi
Publish the config file (if needed):
php artisan vendor:publish --provider="Milestone\LeAPI\LeAPIServiceProvider"
Configuration
Update .env with your ePlus API credentials:
LEAPI_CLIENT_ID=your_client_id
LEAPI_CLIENT_SECRET=your_client_secret
LEAPI_BASE_URL=https://api.epus.com
First Use Case: Authentication Fetch an access token and initialize the client:
use Milestone\LeAPI\Facades\LeAPI;
$token = LeAPI::auth()->getToken();
$client = LeAPI::client($token);
Quick API Call Example: Fetch a customer list:
$customers = $client->get('/customers');
Authentication Handling
LeAPI::auth() to manage tokens (refresh, store, retrieve).$token = LeAPI::auth()->getToken(true); // Force refresh if expired
API Resource Management
// Create
$response = $client->post('/customers', $data);
// Read
$customer = $client->get('/customers/123');
// Update
$client->put('/customers/123', $data);
// Delete
$client->delete('/customers/123');
->paginate() helper:
$customers = $client->get('/customers')->paginate();
Webhook Integration
LeAPI::webhooks() facade:
$webhook = LeAPI::webhooks()->subscribe('customer.created', route('webhook.handle'));
Middleware for API Calls
$client = LeAPI::client($token)
->withMiddleware(new \Milestone\LeAPI\Middleware\LogRequests);
$this->app->singleton(\Milestone\LeAPI\Client::class, function ($app) {
$token = LeAPI::auth()->getToken();
return LeAPI::client($token);
});
$client->onResponse(function ($response) {
event(new \App\Events\EPlusApiResponse($response));
});
LeAPI::fake() helper for mocking API calls in tests:
LeAPI::fake()->shouldReceive('get')->once()->andReturn(['data' => []]);
Token Expiry
TokenExpiredException when calling authenticated endpoints.LeAPI::auth()->refreshToken() in a try-catch block:
try {
$client->get('/customers');
} catch (\Milestone\LeAPI\Exceptions\TokenExpiredException $e) {
LeAPI::auth()->refreshToken();
retry();
}
Rate Limiting
HttpException manually:
$client->withMiddleware(new \Milestone\LeAPI\Middleware\RetryOnRateLimit);
Endpoint Changes
if (!$response->successful()) {
throw new \RuntimeException('API Error: ' . $response->getBody());
}
Config Overrides
config/leapi.php) for environment-specific overrides.LEAPI_DEBUG=true in .env to log raw API responses.\Milestone\LeAPI\Middleware\LogRequests to inspect requests/responses:
$client->withMiddleware(new \Milestone\LeAPI\Middleware\LogRequests);
Custom Responses
Extend the Milestone\LeAPI\Response class to add domain-specific logic:
class CustomResponse extends \Milestone\LeAPI\Response {
public function asCustomer() {
return $this->data['customer'];
}
}
Register the binding in a service provider:
$this->app->bind(
\Milestone\LeAPI\Response::class,
\App\Extensions\CustomResponse::class
);
New API Methods
Add custom methods to the Client class via traits or decorators:
use Milestone\LeAPI\Traits\ApiMethods;
class CustomClient extends \Milestone\LeAPI\Client {
use ApiMethods;
public function getCustomerOrders($customerId) {
return $this->get("/customers/{$customerId}/orders");
}
}
Webhook Verification
Extend the Webhook class to validate payloads:
class CustomWebhook extends \Milestone\LeAPI\Webhook {
public function verifyPayload(array $payload) {
// Custom validation logic
return true;
}
}
How can I help you explore Laravel packages today?