How to Integrate OKX API in Laravel (V5) β Full Setup Guide Want to connect to OKX and fetch crypto account balances or place trades directly from your Laravel application? In this guide, youβll learn how to set up OKX API integration in Laravel using secure API authentication with HMAC signatures.
π Prerequisites
- Laravel 7+ installed
- OKX account with API keys created
- Basic understanding of Laravel services and HTTP requests
π§± Step 1: Create Your OKX API Keys
- Go to https://www.okx.com
- Login β Click Profile Icon > API
- Click Create V5 API Key
- Enter
- API name
- Your custom Passphrase (used in code)
- Permissions (e.g., Read-Only, Trade)
- Whitelist your IP (recommended)
- Copy:
- API Key
- Secret Key
- Your Passphrase
β οΈ Save them securely. Youβll use them in .env.
βοΈ Step 2: Add Keys to .env
OKX_API_KEY=your_api_key
OKX_SECRET_KEY=your_secret_key
OKX_PASSPHRASE=your_passphraseπ Step 3: Create Config File
Create config/okx.php:
<?php
return [
'api_key' => env('OKX_API_KEY'),
'secret_key' => env('OKX_SECRET_KEY'),
'passphrase' => env('OKX_PASSPHRASE'),
];
π§ Step 4: Create OKX Service Class
Create: app/Services/OkxService.php
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class OkxService
{
protected $apiKey;
protected $secretKey;
protected $passphrase;
public function __construct()
{
$this->apiKey = config('okx.api_key');
$this->secretKey = config('okx.secret_key');
$this->passphrase = config('okx.passphrase');
// dd($this->apiKey, $this->secretKey, $this->passphrase);
}
protected function sign($timestamp, $method, $requestPath, $body = '')
{
$preHash = $timestamp . $method . $requestPath . $body;
return base64_encode(hash_hmac('sha256', $preHash, $this->secretKey, true));
}
public function getBalance()
{
$method = 'GET';
$requestPath = '/api/v5/account/balance';
$body = '';
// $timestamp = now()->toIso8601String();
$timestamp = gmdate('Y-m-d\TH:i:s.v\Z');
$sign = $this->sign($timestamp, $method, $requestPath, $body);
$response = Http::withHeaders([
'OK-ACCESS-KEY' => $this->apiKey,
'OK-ACCESS-SIGN' => $sign,
'OK-ACCESS-TIMESTAMP' => $timestamp,
'OK-ACCESS-PASSPHRASE' => $this->passphrase,
'Content-Type' => 'application/json',
])->get("https://www.okx.com$requestPath");
return $response->json();
}
}
π§ͺ Step 5: Create Controller to Use Service
php artisan make:controller OkxController
Update: app/Http/Controllers/OkxController.php
<?php
namespace App\Http\Controllers;
use App\Services\OkxService;
class OkxController extends Controller
{
public function checkBalance(OkxService $okxService)
{
$balance = $okxService->getBalance();
// For development
dd($balance);
// Or return JSON
// return response()->json($balance);
}
}
π Step 6: Define Route
In routes/web.php (or api.php):
use App\Http\Controllers\OkxController;
Route::get('/okx/balance', [OkxController::class, 'checkBalance']);
β
Final Test
Visit:
http://127.0.0.1:8000/okx/balance
Security Tips
Use IP whitelisting for your keys from the OKX dashboard.
Use .env for sensitive data.
Never expose your API keys to the frontend.
To use the OKX Affiliate API,
you must be an approved affiliate partner and have access to the Affiliate API endpoints (as listed in the OKX V5 Docs β Affiliate section).
β
What You Can Do with the Affiliate API
You can:
Get invitee info using their UID
Track invited user activity
Power yourπ Requirements Before Using the API
β
Be registered as an OKX Affiliate
π Contact OKX BD (Business Development) team to be approved.
β
Have a valid OKX API key, secret, and passphrase with Affiliate API permissions enabled.
β
Use correct headers with HMAC SHA256 signature, same as other OKX API calls. affiliate dashboard or reporting system
π Authentication Overview
Affiliate API requires the same authentication format as the standard OKX API:
OK-ACCESS-KEY: Your API Key
OK-ACCESS-SIGN: Signature (HMAC of timestamp + method + endpoint + body)
OK-ACCESS-TIMESTAMP: ISO 8601 UTC with milliseconds
OK-ACCESS-PASSPHRASE: Your API passphrase
Example Endpoint
From the docs:
π GET /api/v5/affiliate/invitee-info
Query invitee info by their UID.
π Request Example (GET)
GET https://www.okx.com/api/v5/affiliate/invitee-info?uid=your-invitee-uidπ§ͺ Laravel Example: Call OKX Affiliate API
Hereβs how to use this endpoint in a Laravel service.
π OkxAffiliateService.php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class OkxAffiliateService
{
protected $apiKey;
protected $secretKey;
protected $passphrase;
public function __construct()
{
$this->apiKey = config('okx.api_key');
$this->secretKey = config('okx.secret_key');
$this->passphrase = config('okx.passphrase');
}
protected function sign($timestamp, $method, $requestPath, $body = '')
{
$preHash = $timestamp . $method . $requestPath . $body;
return base64_encode(hash_hmac('sha256', $preHash, $this->secretKey, true));
}
public function getInviteeInfo($uid)
{
$method = 'GET';
$requestPath = "/api/v5/affiliate/invitee-info?uid={$uid}";
$body = '';
$timestamp = gmdate('Y-m-d\TH:i:s.v\Z');
$sign = $this->sign($timestamp, $method, "/api/v5/affiliate/invitee-info?uid={$uid}", $body);
$response = Http::withHeaders([
'OK-ACCESS-KEY' => $this->apiKey,
'OK-ACCESS-SIGN' => $sign,
'OK-ACCESS-TIMESTAMP' => $timestamp,
'OK-ACCESS-PASSPHRASE' => $this->passphrase,
'Content-Type' => 'application/json',
])->get("https://www.okx.com$requestPath");
return $response->json();
}
}
π§Ύ Example Usage in Controller
use App\Services\OkxAffiliateService;
public function getInvitee(OkxAffiliateService $service)
{
$uid = '1234567890'; // replace with actual UID
$data = $service->getInviteeInfo($uid);
dd($data);
}π Notes
- You must be an OKX affiliate to use these endpoints.
- If you get errors like Permission Denied, contact your OKX BD (business development) partner to enable access.
- Use whitelisted IP for better security.
π§ Summary
Step Action
1οΈβ£ Get approved as an OKX affiliate
2οΈβ£ Get your API Key, Secret, Passphrase
3οΈβ£ Use Laravel or HTTP client to sign and call the endpoint
4οΈβ£ Contact BD for help with extra data or permissions
π§Ύ Conclusion
You've now integrated the OKX API securely into your Laravel project, using API authentication and custom signature headers. You can expand this by accessing trading endpoints, placing orders, or fetching market data.