Skip to main content

Authentication

Learn how to manage API keys and authenticate with the ZenPays API.

API Key Types​

ZenPays uses two types of API keys:

Key TypePrefixUsage
Livezp_live_Production payments
Testzp_test_Sandbox testing

Getting Your API Key​

  1. Log in to your ZenPays Dashboard
  2. Navigate to Settings → API Keys
  3. Copy your API key
warning

Keep your API keys secure. Never expose them in client-side code or commit them to version control.

Using the SDK​

import { ZenPays } from '@zenxdigitalholdings/zenpays'

const zenpays = new ZenPays({
apiKey: process.env.ZENPAYS_API_KEY!,
})

Environment-Based Authentication​

// Determine environment from API key prefix
function getEnvironment(apiKey: string): 'live' | 'test' {
return apiKey.startsWith('zp_live_') ? 'live' : 'test'
}

const zenpays = new ZenPays({
apiKey: process.env.ZENPAYS_API_KEY!,
})

console.log('Environment:', getEnvironment(process.env.ZENPAYS_API_KEY!))

IP Whitelisting​

For enhanced security, whitelist IPs allowed to use your API keys:

// List whitelisted IPs
const ips = await zenpays.merchants.listWhitelistedIPs()

// Add a new IP
await zenpays.merchants.addIPToWhitelist(
'203.0.113.50',
'Production Server'
)

// Remove an IP
await zenpays.merchants.removeIPFromWhitelist('ip_entry_id')

Two-Factor Authentication​

Enable 2FA for additional security:

// Setup 2FA
const setup = await zenpays.security.setup2FA()
console.log('Scan this QR code:', setup.qrCodeUrl)
console.log('Backup codes:', setup.backupCodes)

// Verify 2FA code
await zenpays.security.verify2FA({ code: '123456' })

// Check 2FA status
const status = await zenpays.security.is2FAEnabled()
console.log('2FA enabled:', status.enabled)

Best Practices​

  1. Use environment variables - Never hardcode API keys
  2. Rotate keys regularly - Create new keys and revoke old ones
  3. Use minimal scopes - Only grant necessary permissions
  4. Enable IP whitelisting - Restrict access to known IPs
  5. Enable 2FA - Add an extra layer of security
  6. Monitor usage - Check API key usage in the dashboard

Handling Authentication Errors​

import { AuthenticationError, AuthorizationError } from '@zenxdigitalholdings/zenpays'

try {
await zenpays.payments.createPaymentIntent({
amount: 1000,
currency: 'USD',
})
}
catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid API key')
}
else if (error instanceof AuthorizationError) {
console.error('API key lacks required permissions')
}
}

Next Steps​