Manage API Keys
Authentication
The SuiPay SDK uses API keys for authentication. This guide explains how to configure authentication in your application using the SDK.
SDK Authentication Setup
The SDK handles authentication automatically once you provide your API key during client initialization. No need to manually set headers or manage tokens.
import { SuiPayClient } from '@suipay/api';
const client = new SuiPayClient({
apiKey: 'sk_your_api_key_here'
});
API Key Format
API keys are prefixed with sk_
and generated using cryptographically secure random bytes. A typical API key looks like:
sk_1234567890abcdef1234567890abcdef
Getting Your API Key
API keys should be created through the SuiPay dashboard:
Login to SuiPay: Visit https://demo2.suipay.net and sign in to your account
Navigate to Dev API Keys: Go to the "Dev API Keys" section in your dashboard
Create New API Key: Click "Create API Key" and give it a descriptive name
Copy and Store: Your API key will be shown once - copy it immediately and store it securely

Configuration Options
Basic Configuration
const client = new SuiPayClient({
apiKey: 'sk_your_api_key_here'
});
Environment Variables
Use environment variables for better security:
// .env file
SUIPAY_API_KEY=sk_your_api_key_here
SUIPAY_BASE_URL=https://api.suipay.net/api/v1 // optional
// In your code
const client = new SuiPayClient({
apiKey: process.env.SUIPAY_API_KEY!,
baseUrl: process.env.SUIPAY_BASE_URL // optional
});
Testing Authentication
Use the ping()
method to verify your API key is working:
try {
const result = await client.ping();
if (result.success) {
console.log(`✅ Authentication successful! User ID: ${result.user_id}`);
} else {
console.log('❌ Authentication failed');
}
} catch (error) {
console.error('Authentication error:', error);
}
User Access Control
API keys are scoped to specific users and their resources:
Each API key can only access the user account it was created for
You cannot access other users' data with your API key
All resources (payment links, withdrawals, bank accounts) are tied to the user associated with your API key
Example access control:
API Key A is created for User 1
API Key B is created for User 2
API Key A can only access User 1's data and resources
API Key B can only access User 2's data and resources
Authentication Errors
The SDK provides specific error types for authentication issues:
import { SuiPayAuthenticationError } from '@suipay/api';
try {
const user = await client.user.get();
} catch (error) {
if (error instanceof SuiPayAuthenticationError) {
console.error('Authentication failed:', error.message);
// Possible causes:
// - Invalid API key
// - API key doesn't exist
// - API key has been deactivated
}
}
Common Authentication Errors
Invalid API key
API key format is incorrect
Ensure API key starts with sk_
API key not found
API key doesn't exist
Check your API key in the dashboard
API key deactivated
API key has been disabled
Create a new API key
Unauthorized
Missing or malformed API key
Verify client initialization
Security Best Practices
1. Environment Variables
// ✅ Good
const client = new SuiPayClient({
apiKey: process.env.SUIPAY_API_KEY!
});
// ❌ Bad - hardcoded in source
const client = new SuiPayClient({
apiKey: 'sk_1234567890abcdef...'
});
2. Separate Keys per Environment
// ✅ Good - separate keys
const config = {
development: {
apiKey: process.env.SUIPAY_DEV_API_KEY!,
baseUrl: 'http://localhost:3001/api/v1'
},
production: {
apiKey: process.env.SUIPAY_PROD_API_KEY!,
baseUrl: 'https://api.suipay.net/api/v1'
}
};
const client = new SuiPayClient(config[process.env.NODE_ENV!]);
3. Key Validation
function createSuiPayClient(apiKey: string) {
if (!apiKey) {
throw new Error('API key is required');
}
if (!apiKey.startsWith('sk_')) {
throw new Error('Invalid API key format');
}
return new SuiPayClient({ apiKey });
}
4. Error Handling
async function initializeClient() {
try {
const client = new SuiPayClient({
apiKey: process.env.SUIPAY_API_KEY!
});
// Test the connection
const result = await client.ping();
if (!result.success) {
throw new Error('Failed to authenticate with SuiPay API');
}
return client;
} catch (error) {
console.error('Failed to initialize SuiPay client:', error);
throw error;
}
}
Best Practices Summary
Store API keys securely using environment variables
Never commit API keys to version control
Use different API keys for development and production
Test authentication before using the client
Handle authentication errors gracefully
Rotate API keys regularly
Monitor API key usage in the dashboard
Deactivate unused API keys promptly
Last updated