Overview
This guide provides detailed instructions for implementing HMAC-SHA256 signatures to authenticate requests to the HopNow API.For a comprehensive authentication guide, see our Authentication documentation.
What is HMAC?
HMAC (Hash-based Message Authentication Code) is a cryptographic technique that ensures:- Authentication: Verifies the request comes from a legitimate source
- Integrity: Confirms the request hasn’t been tampered with
- Non-repudiation: Prevents denial of having sent the request
Step-by-Step Implementation
Step 1: Prepare the Payload
The HMAC signature is calculated over a payload consisting of:Components Breakdown
| Component | Description | Example |
|---|---|---|
HTTP_METHOD | HTTP method in UPPERCASE | POST |
FULL_URL | Complete URL with protocol | https://apis.hopnow.io/v1/customers/cus_123/accounts |
TIMESTAMP | Unix timestamp as string | 1640995200 |
REQUEST_BODY | JSON request body (empty for GET) | {"name":"My Account"} |
Example Payload
For this request:Step 2: Generate the Signature
Use HMAC-SHA256 with your API secret to sign the payload:Step 3: Include Authentication Headers
Add the required headers to your request:Common Implementation Mistakes
1. Incorrect Payload Construction
❌ Wrong: Including spaces or formatting2. Wrong HTTP Method Case
❌ Wrong: Using lowercase method3. JSON Body Formatting Issues
❌ Wrong: Pretty-printed JSON4. Timestamp Mismatch
❌ Wrong: Using different timestampsTesting Your Implementation
Verification Steps
- Log the payload before signing to verify format
- Check timestamp is within 5 minutes of current time
- Verify signature matches expected output
- Test with known values using the examples below
Test Vectors
Use these test vectors to verify your implementation:Test Case 1: Simple POST Request
Input:- Method:
POST - URL:
https://apis.hopnow.io/v1/test - Timestamp:
1640995200 - Body:
{"test":true} - Secret:
test_secret_key_123
Test Case 2: GET Request (Empty Body)
Input:- Method:
GET - URL:
https://apis.hopnow.io/v1/customers/cus_123/accounts - Timestamp:
1640995200 - Body: “ (empty string)
- Secret:
test_secret_key_123
Debug Helper Function
Use this function to debug signature generation:Security Best Practices
1. Secure Secret Storage
- Store API secrets in environment variables or secure key management
- Never hardcode secrets in your application code
- Use different secrets for different environments
2. Timestamp Management
- Use current timestamp for each request (don’t reuse)
- Ensure your server clock is synchronized
- Handle timezone properly (always use UTC)
3. Request Handling
- Generate signatures just before sending requests
- Don’t cache or reuse signatures
- Implement proper error handling for authentication failures
4. Logging and Debugging
- Log payload construction for debugging (but never log secrets)
- Monitor authentication failures
- Implement retry logic with exponential backoff
Troubleshooting
Signature Verification Failures
If you’re getting authentication errors:- Double-check payload construction - Use the debug helper
- Verify timestamp is recent - Should be within 5 minutes
- Check HTTP method case - Must be uppercase in payload
- Validate JSON formatting - Should be compact, no extra whitespace
- Confirm URL is complete - Include full URL with https://
Common Error Messages
| Error | Likely Cause | Solution |
|---|---|---|
invalid_signature | Payload construction error | Use debug helper to verify payload |
timestamp_too_old | Timestamp too old | Use current timestamp |
missing_headers | Header not included | Include all three required headers |
Need additional help with HMAC implementation? Contact our support team with your debug output.