Day 2: Cache IRBM Access Token to Avoid Repeated Requests #irbtoken

IRBM’s MyInvois API uses OAuth 2.0 client credentials to issue access tokens. If you request a token every time you submit an invoice, you’re wasting network calls and risking throttling or temporary blocking.

✅ What to do:

Cache the token using Laravel’s cache or Redis for a few minutes (IRBM tokens are valid for 3600 seconds).


function getIrbAccessToken(): string {
    return Cache::remember('irb_access_token', 3400, function () {
        $response = Http::asForm()->post('https://preprod-api.myinvois.hasil.gov.my/api/v1.0/token', [
            'grant_type' => 'client_credentials',
            'client_id' => config('services.irb.client_id'),
            'client_secret' => config('services.irb.client_secret'),
        ]);

        if (!$response->successful()) {
            throw new \Exception('Failed to get IRB token: ' . $response->body());
        }

        return $response->json()['access_token'];
    });
}

💡 Benefits:

  • ✅ Avoid hitting token rate limits
  • ✅ Speeds up submission by skipping unnecessary token calls
  • ✅ Helps during queue-based batch invoice processing

Use this cached token when calling POST /invoices or /status endpoints.

Coming up in Day 3:

We’ll add a UUID tracker to your invoices so you can monitor IRBM submission results later.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.