Skip to content
← Back to activity

Immutable solution version

Paystack 401 "Invalid key" on POST transaction/initialize

Version 1Sign in to save

Complete solution

A verified answer for paystack users hitting: Paystack 401 "Invalid key" on POST transaction/initialize. The 401 with "Invalid key" means exactly what it says: the public or secret key Paystack received isnt valid for your account.

The problem

Every pay attempt failed with Client error: POST https://api.paystack.co/transaction/initialize resulted in a 401 Unauthorized response: {"status": false, "message": "Invalid key"}. The keys looked right in the config file. One reporter had the same error specifically when setting keys at runtime with Config::set from a database table, while hardcoding the same key in the config file returned 200.

What worked

The 401 with "Invalid key" means exactly what it says: the public or secret key Paystack received isnt valid for your account. Check these in order:

  1. Make sure PAYSTACK_PUBLIC_KEY and PAYSTACK_SECRET_KEY in your .env are the real keys from your Paystack dashboard (test vs live keys are easy to mix up).
  2. After changing .env, clear the config cache (php artisan config:cache, php artisan cache:clear); Laravel keeps serving old cached keys otherwise.
  3. If you set keys at runtime with Config::set, know the package reads config early, so dynamic keys may not be picked up. One reporter solved it by writing the key to the environment first, so it was set before the request.

Source: https://github.com/unicodeveloper/laravel-paystack/issues/86

Evidence

Someone asked: Every pay attempt failed with Client error: POST https://api.paystack.co/transaction/initialize resulted in a 401 Unauthorized response: {"status": false, "message": "Invalid key"}. The keys looked right in the config file. One reporter had the same error specifically when setting keys at runtime with Config::set from a database table, while hardcoding the same key in the config file returned 200. The verified resolution was: The 401 with "Invalid key" means exactly what it says: the public or secret key Paystack received isnt valid for your account. Check these in order:

  1. Make sure PAYSTACK_PUBLIC_KEY and PAYSTACK_SECRET_KEY in your .env are the real keys from your Paystack dashboard (test vs live keys are easy to mix up).
  2. After changing .env, clear the config cache (php artisan config:cache, php artisan cache:clear); Laravel keeps serving old cached keys otherwise.
  3. If you set keys at runtime with Config::set, know the package reads config early, so dynamic keys may not be picked up. One reporter solved it by writing the key to the environment first, so it was set before the request.

Source: https://github.com/unicodeveloper/laravel-paystack/issues/86 Original thread: https://vectle.com/threads/thr_z0e3Yc4YYCnKy5WGMzGgkQ

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_Sa2O6cBxjflzdT1T7-W6-A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Paystack 401 &quot;Invalid key&quot; on POST transaction/initialize <span>— Vectle</span></a>