Getting Your API Keys
To use the SDK, you’ll need to obtain your API keys from the Paystack Dashboard:Log in to Paystack
Visit https://dashboard.paystack.com and sign in to your account.
Test vs Live Keys
Paystack provides two types of secret keys to separate development and production environments:Test Keys
- Prefix:
sk_test_ - Use for development and testing
- No real money is processed
- Safe to experiment with
Live Keys
- Prefix:
sk_live_ - Use for production
- Processes real transactions
- Requires extra security measures
Key Format Validation
The SDK automatically validates your API key format when initializing the Paystack client. Here’s the validation logic fromsrc/index.ts:34-42:
src/index.ts
Basic Setup
Initialize the SDK by passing your secret key to thePaystack constructor:
Environment Variables
The recommended approach is to store your API keys in environment variables rather than hardcoding them:Environment-Based Configuration
You can dynamically select keys based on your environment:How Authentication Works
Under the hood, the SDK uses Bearer token authentication. Fromsrc/main/fetcher.ts:38-39:
src/main/fetcher.ts
Authorization header. You don’t need to manually add authentication to individual requests.
Security Best Practices
Never commit keys to version control
Never commit keys to version control
Add Use environment variables or secret management services instead.
.env files to your .gitignore:.gitignore
Use different keys for each environment
Use different keys for each environment
Maintain separate API keys for:
- Local development (test key)
- Staging/QA (test key)
- Production (live key)
Rotate keys regularly
Rotate keys regularly
Periodically regenerate your API keys, especially if:
- A team member with access leaves
- You suspect a key may have been compromised
- As part of routine security maintenance
Restrict key access
Restrict key access
- Limit which team members can view live keys
- Use secret management tools (AWS Secrets Manager, HashiCorp Vault, etc.)
- Never log or display secret keys in application logs
- Avoid sending keys via email or chat
Server-side only
Server-side only
Secret keys should never be used in:
- Frontend JavaScript (React, Vue, etc.)
- Mobile apps (iOS, Android)
- Browser extensions
- Any client-side code
Multi-Account Support
If you need to work with multiple Paystack accounts, create separate instances:Troubleshooting
Error: Invalid secret key
Error: Invalid secret key
Cause: The provided key doesn’t start with
sk_test_ or sk_live_.Solution:- Verify you’re using the secret key, not the public key (which starts with
pk_) - Check for typos or extra whitespace
- Ensure the key is copied completely from the dashboard
401 Unauthorized errors
401 Unauthorized errors
Environment variable not found
Environment variable not found
Cause: The environment variable isn’t loaded or is undefined.Solution:
- Ensure your
.envfile is in the correct location - Check if you need to install
dotenv(Node.js):npm install dotenv - Load environment variables:
import 'dotenv/config'(Node.js) - Restart your development server after changing
.env

