...

How to Integrate OKX API in Laravel – Step-by-Step Guide

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

  1. Go to https://www.okx.com
  2. Login β†’ Click Profile Icon > API
  3. Click Create V5 API Key
  4. Enter
    • API name
    • Your custom Passphrase (used in code)
    • Permissions (e.g., Read-Only, Trade)
    • Whitelist your IP (recommended)
  5. 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.



William Anderson

I am a versatile Full-Stack Web Developer with a strong focus on Laravel, Livewire, Vue.js, and Tailwind CSS. With extensive experience in backend development, I specialize in building scalable, efficient, and high-performance web applications.