API Keys
API keys are essential for authenticating with Moosyl's services. This guide covers how to manage, secure, and use your API keys effectively.
Overview
Moosyl provides two types of API keys:
- Publishable Keys (
pk_...): Safe for client-side use (Flutter, React apps) - Secret Keys (
sk_...): Must be kept secure, only for backend use
Getting Your API Keys
1. Sign Up for Moosyl
- Visit moosyl.com and create an account
- Complete the account verification process
- Access your dashboard
2. Generate API Keys
- Navigate to API Keys in your dashboard
- Click "Create New Key"
- Choose key type:
- Publishable Key (for client-side)
- Secret Key (for backend)
- Copy and store securely
3. Test vs Live Keys
- Test Keys (
pk_test_...,sk_test_...): For development and testing - Live Keys (
pk_live_...,sk_live_...): For production transactions
Key Types Explained
Publishable Keys
Format: pk_test_... or pk_live_...
Usage:
- Initialize payment interfaces in Flutter/React apps
- Safe to expose in client-side code
- Cannot perform sensitive operations
Example:
// Flutter
MoosylView(
publishableApiKey: 'pk_test_1234567890abcdef',
transactionId: 'txn_123',
// ... other properties
)// React
<MoosylProvider publishableKey="pk_test_1234567890abcdef">
<PaymentForm />
</MoosylProvider>Secret Keys
Format: sk_test_... or sk_live_...
Usage:
- Create payment requests
- Verify webhook signatures
- Access sensitive account data
- Must be kept secure
Example:
// Node.js Backend
const response = await fetch('https://api.moosyl.com/payment-request', {
method: 'POST',
headers: {
'Authorization': 'sk_test_1234567890abcdef',
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount: 5000,
transactionId: 'txn_123',
}),
});Security Best Practices
🔒 Keep Secret Keys Secure
❌ Never do this:
// DON'T: Expose secret key in client code
const secretKey = 'sk_live_1234567890abcdef'; // NEVER!✅ Do this instead:
// DO: Use environment variables
const secretKey = process.env.MOOSYL_SECRET_KEY;🔄 Rotate Keys Regularly
- Generate new keys in your dashboard
- Update your applications with new keys
- Revoke old keys after confirming new ones work
- Test thoroughly before revoking old keys
🏗️ Use Different Keys for Different Environments
Development:
MOOSYL_SECRET_KEY=sk_test_dev_1234567890abcdef
MOOSYL_PUBLISHABLE_KEY=pk_test_dev_1234567890abcdefStaging:
MOOSYL_SECRET_KEY=sk_test_staging_1234567890abcdef
MOOSYL_PUBLISHABLE_KEY=pk_test_staging_1234567890abcdefProduction:
MOOSYL_SECRET_KEY=sk_live_1234567890abcdef
MOOSYL_PUBLISHABLE_KEY=pk_live_1234567890abcdef🔍 Monitor Key Usage
- Check dashboard logs for API key usage
- Set up alerts for unusual activity
- Review access patterns regularly
- Monitor failed authentication attempts
Environment Setup
Flutter/React Apps
Environment Variables (.env file):
MOOSYL_PUBLISHABLE_KEY=pk_test_1234567890abcdefUsage:
// Flutter
final publishableKey = const String.fromEnvironment('MOOSYL_PUBLISHABLE_KEY');// React
const publishableKey = process.env.REACT_APP_MOOSYL_PUBLISHABLE_KEY;Backend Services
Environment Variables:
MOOSYL_SECRET_KEY=sk_test_1234567890abcdef
MOOSYL_PUBLISHABLE_KEY=pk_test_1234567890abcdefUsage:
// Node.js
const secretKey = process.env.MOOSYL_SECRET_KEY;
const publishableKey = process.env.MOOSYL_PUBLISHABLE_KEY;# Python
import os
secret_key = os.getenv('MOOSYL_SECRET_KEY')
publishable_key = os.getenv('MOOSYL_PUBLISHABLE_KEY')Key Management
Creating New Keys
- Log into your dashboard
- Go to API Keys section
- Click "Create New Key"
- Select key type and environment
- Set expiration date (optional)
- Copy the key immediately
Revoking Keys
- Find the key in your dashboard
- Click "Revoke"
- Confirm the action
- Update your applications with new keys
Key Permissions
You can restrict key permissions for enhanced security:
- Payment Creation: Create payment requests
- Webhook Verification: Verify webhook signatures
- Account Access: Read account information
- Transaction History: Access transaction data
Testing Your Keys
Verify Publishable Key
// Test if publishable key is valid
const response = await fetch('https://api.moosyl.com/account', {
headers: {
'Authorization': 'pk_test_1234567890abcdef',
},
});
if (response.ok) {
console.log('Publishable key is valid');
} else {
console.log('Invalid publishable key');
}Verify Secret Key
// Test if secret key is valid
const response = await fetch('https://api.moosyl.com/payment-request', {
method: 'POST',
headers: {
'Authorization': 'sk_test_1234567890abcdef',
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount: 100, // Small test amount
transactionId: 'test_' + Date.now(),
}),
});
if (response.ok) {
console.log('Secret key is valid');
} else {
console.log('Invalid secret key');
}Common Issues
Invalid API Key Error
Symptoms:
401 Unauthorizedresponses- "Invalid API key" error messages
Solutions:
- Check key format: Ensure correct prefix (
pk_orsk_) - Verify environment: Make sure you're using the right environment keys
- Check expiration: Keys may have expired
- Confirm permissions: Key may lack required permissions
Key Not Working in Production
Common causes:
- Using test keys in production
- Missing environment variables
- Incorrect key format
- Network/firewall issues
Debugging steps:
- Verify key type: Use live keys for production
- Check environment setup: Ensure variables are loaded
- Test with curl: Verify key works independently
- Check logs: Review application and API logs
Webhook Signature Verification Fails
Symptoms:
- Webhook signatures don't match
Invalid signatureerrors
Solutions:
- Use correct secret key: Must match the webhook's secret
- Check payload format: Use raw request body
- Verify algorithm: Use HMAC-SHA256
- Check timing: Ensure clocks are synchronized
Key Rotation Strategy
Planned Rotation
- Schedule rotation (e.g., every 90 days)
- Generate new keys before expiration
- Update applications gradually
- Monitor for issues
- Revoke old keys after confirmation
Emergency Rotation
- Immediately revoke compromised keys
- Generate new keys
- Update all applications
- Monitor for unauthorized access
- Review security logs
Next Steps
Now that you understand API keys:
- Set up your first integration using our Flutter SDK or React SDK
- Configure webhooks for real-time payment notifications
- Learn about webhook security in our webhooks guide
- Review our FAQ for common questions
- Test your integration thoroughly before going live
Support
Need help with API keys?
- Check our FAQ for common issues
- Email support: support@moosyl.com
- Dashboard support: Use the built-in support system
- Review logs: Check API usage in your dashboard