API Signature
Overview
This document describes the signature verification mechanism for TocoPay API. All API requests require signature verification to ensure the security and integrity of requests.
Signature Algorithm
Signature Steps
- Collect Parameters: Collect all request parameters (including URL query parameters and request body parameters)
- Remove Signature Field: If the request contains a
signfield, remove it first - Add Timestamp: Add a
timestampfield (Unix timestamp in seconds) - Sort Parameters: Sort all parameters alphabetically by key name (A -> Z)
- Build Signature String: Concatenate parameters in
key=value&format - Add Secret Key: Append
&key=your_api_secretto the end of the signature string - Generate Signature: Perform MD5 encryption on the signature string and convert to uppercase
Signature String Format
param1=value1¶m2=value2×tamp=1234567890&key=your_api_secret
Important Notes
- Empty value parameters (
null,undefined, empty strings) will be ignored - Object and array type parameters will be converted to JSON strings
- Signature result must be in uppercase format
- Timestamp uses Unix timestamp (in seconds)
Multi-Language Implementation Examples
JavaScript (Node.js)
const crypto = require('crypto');
function generateSignature(params, apiSecret) {
// Remove existing sign field
delete params.sign;
// Add timestamp
params.timestamp = Math.floor(Date.now() / 1000);
// Sort by key name
const keys = Object.keys(params).sort();
// Build signature string
let dataString = '';
keys.forEach(key => {
const value = params[key];
if (value !== null && value !== undefined && value !== '') {
// Convert objects or arrays to JSON
const stringValue = (typeof value === 'object') ? JSON.stringify(value) : value;
dataString += key + '=' + stringValue + '&';
}
});
// Add secret key
dataString += 'key=' + apiSecret;
// Generate MD5 signature and convert to uppercase
const sign = crypto.createHash('md5').update(dataString).digest('hex').toUpperCase();
return sign;
}
// Usage example
const params = {
uid: 'your_merchant_id',
amount: 100.00,
currency: 'USD',
order_id: 'ORDER123456'
};
const apiSecret = 'your_api_secret';
const signature = generateSignature(params, apiSecret);
params.sign = signature;
console.log('Signature result:', signature);
console.log('Complete parameters:', params);Request Examples
Complete API Request Example
curl -X POST "https://api.tocopay.com/pay" \
-H "Content-Type: application/json" \
-d '{
"uid": "your_merchant_id",
"amount": 100.00,
"currency": "USD",
"order_id": "ORDER123456",
"timestamp": 1640995200,
"sign": "A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6"
}'Error Handling
Common Error Codes
20041: Missing signature parameter20042: Signature verification failed20005: Missing merchant ID30001: Merchant does not exist
Debugging Tips
- Check Parameter Completeness: Ensure all required parameters are included
- Verify Timestamp: Ensure timestamp format is correct (Unix timestamp in seconds)
- Check Parameter Sorting: Ensure parameters are sorted alphabetically
- Verify Signature Algorithm: Ensure MD5 encryption is used and converted to uppercase
- Check Secret Key: Ensure the correct API secret key is used
Security Considerations
- Protect API Secret: API secrets should be stored securely and not exposed in client-side code
- Use HTTPS: All API requests should be made over HTTPS
- Timestamp Validation: Consider validating request timestamps to prevent replay attacks
- IP Whitelist: Consider configuring IP whitelist for enhanced security
Support
If you encounter any issues while implementing signature verification, please contact our technical support team.
Updated 4 months ago