Generating an API Key
Step 1: Navigate to API Integration
- Log in to your NestSMS Dashboard
- In the left sidebar, go to API Integration → API Token
- Click the Add Token button
Step 2: Configure Your Key
Enter a descriptive name for your API key to help you identify its purpose later.
Optionally, you can configure:
- IP Whitelist: Restrict the key to specific IP addresses
- Permissions: Limit what the key can do (send SMS, check balance, etc.)
- Expiry Date: Set an expiration date for the key
Step 3: Copy Your API Key
Once generated, your API key will be displayed. Copy it immediately — it will only be shown once for security reasons.
nsms_live_abc123xyz789def456ghi012jkl345mno678Using Your API Key
Include your API key in every request using one of these methods:
Method 1: X-API-Key Header (Recommended)
curl -X POST https://api.nestsms.com/api/v1/sms/send \
-H "X-API-Key: nsms_live_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"to": "9840123456", "message": "Test SMS"}'Method 2: Authorization Bearer Token
curl -X POST https://api.nestsms.com/api/v1/sms/send \
-H "Authorization: Bearer nsms_live_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"to": "9840123456", "message": "Test SMS"}'Key Prefixes
NestSMS API keys use prefixes to indicate their environment:
| Prefix | Environment | Description |
|---|---|---|
nsms_live_ | Production | Live key that charges your real balance and sends actual SMS messages |
nsms_test_ | Development | Test key for development - no charges, simulated delivery |
Storing API Keys Securely
Environment Variables
Never hardcode API keys in your source code. Use environment variables instead:
# .env
NESTSMS_API_KEY=nsms_live_your_api_key_hereIn Your Application
Access the key from environment variables:
// Node.js / Next.js
const apiKey = process.env.NESTSMS_API_KEY;
// Python
import os
api_key = os.getenv('NESTSMS_API_KEY')
// PHP
$apiKey = $_ENV['NESTSMS_API_KEY'];Managing API Keys
Viewing Your Keys
Go to API Integration → API Token to see all your active keys. You'll see:
- Key name
- Partial key (last 8 characters)
- Creation date
- Last used date
- Permissions
Revoking Keys
To revoke a compromised or unused key:
- Go to API Integration → API Token
- Find the key you want to revoke
- Click the Delete or Revoke button
- Confirm the action
Revoked keys are immediately invalidated and cannot be recovered.
Rotating Keys
For security best practices, rotate your API keys regularly:
- Generate a new API key
- Update your application with the new key
- Test thoroughly to ensure everything works
- Once verified, revoke the old key
IP Whitelisting
Enhance security by restricting which IP addresses can use your API key:
- Edit your API key settings
- Add allowed IP addresses (comma-separated)
- Save changes
Example: 203.0.113.1, 203.0.113.2
Permissions
You can create keys with limited permissions for specific use cases:
| Permission | Description | Use Case |
|---|---|---|
| Send SMS | Send single or bulk SMS | Production applications |
| Check Balance | View wallet balance | Monitoring systems |
| View Reports | Access delivery reports | Analytics dashboards |
| Manage Templates | Create/edit templates | Template management tools |
| Full Access | All permissions | Admin integrations |
Troubleshooting
Invalid API Key
{
"success": false,
"error": "Invalid API key",
"code": "INVALID_API_KEY"
}Solutions:
- Verify the key is correct (check for typos or extra spaces)
- Ensure the key hasn't been revoked
- Check that you're using the correct environment (test vs live)
API Key Expired
{
"success": false,
"error": "API key has expired",
"code": "API_KEY_EXPIRED"
}Solutions:
- Generate a new API key
- Remove expiry dates from keys that should remain active
IP Restricted
{
"success": false,
"error": "Request from non-whitelisted IP",
"code": "IP_RESTRICTED"
}Solutions:
- Add your current IP to the whitelist
- Remove IP restrictions if not needed
- Use a VPN or proxy from an allowed IP
Best Practices
- Use Descriptive Names: Name keys based on their purpose (e.g., "Production API", "Staging Server")
- Principle of Least Privilege: Grant only necessary permissions
- Regular Rotation: Rotate keys every 90 days
- Monitor Usage: Check last used dates regularly
- Separate Environments: Use different keys for dev, staging, and production
- Set Expiry Dates: For temporary integrations or testing
- Enable IP Whitelisting: For server-to-server communications