- Create API gateway service with Express.js - Implement sliding window rate limiting via Redis - Add API key management with tiered access (free/developer/enterprise) - Track usage analytics per key and globally - Add RPC proxy to blockchain nodes - Configure Docker Compose with api-gateway and redis services - Free tier: 100 req/min, Developer: 1000 req/min, Enterprise: unlimited
4.8 KiB
4.8 KiB
Synor Public API Gateway
Rate-limited, authenticated access to Synor blockchain RPC.
Quick Start
# Start with API profile
docker compose -f docker-compose.testnet.yml --profile api up -d
# Test the API
curl http://localhost:17400/health
# Make an RPC call (anonymous - 100 req/min)
curl -X POST http://localhost:17400/rpc \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"synor_getBlockCount","params":[],"id":1}'
Rate Limit Tiers
| Tier | Rate Limit | Price | Features |
|---|---|---|---|
| Free | 100 req/min | $0 | Anonymous or API key |
| Developer | 1000 req/min | $49/mo | API key + analytics |
| Enterprise | Unlimited | Custom | SLA, dedicated support |
Authentication
API Key Header
curl -X POST http://localhost:17400/rpc \
-H "Authorization: Bearer sk_developer_abc123..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"synor_getBlockCount","params":[],"id":1}'
X-API-Key Header
curl -X POST http://localhost:17400/rpc \
-H "X-API-Key: sk_developer_abc123..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"synor_getBlockCount","params":[],"id":1}'
Query Parameter
curl -X POST "http://localhost:17400/rpc?api_key=sk_developer_abc123..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"synor_getBlockCount","params":[],"id":1}'
Rate Limit Headers
All RPC responses include rate limit information:
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 999
X-RateLimit-Reset: 1704067260
X-RateLimit-Tier: developer
API Endpoints
Public Endpoints
Health Check
GET /health
Returns service health status.
JSON-RPC Proxy
POST /rpc
Proxies JSON-RPC requests to the Synor node.
Admin Endpoints
Requires Authorization: Bearer <ADMIN_KEY> header.
Create API Key
POST /v1/keys
Content-Type: application/json
{
"tier": "developer",
"name": "My App"
}
Response:
{
"key": "sk_developer_abc123...",
"tier": "developer",
"name": "My App",
"createdAt": 1704067200000,
"lastUsed": 0,
"requestCount": 0
}
List API Keys
GET /v1/keys
Get Key Stats
GET /v1/keys/:key/stats
(Accessible by key owner or admin)
Revoke API Key
DELETE /v1/keys/:key
Error Responses
Rate Limit Exceeded (429)
{
"jsonrpc": "2.0",
"error": {
"code": -32005,
"message": "Rate limit exceeded",
"data": {
"retryAfter": 45,
"tier": "free",
"upgrade": "Upgrade to Developer tier for 1000 req/min"
}
},
"id": null
}
Invalid API Key (401)
{
"jsonrpc": "2.0",
"error": {
"code": -32001,
"message": "Invalid API key"
},
"id": null
}
RPC Node Unavailable (502)
{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "RPC node unavailable"
},
"id": null
}
Supported RPC Methods
Chain Methods
synor_getBlockCount- Get current block heightsynor_getBlockHash- Get block hash by heightsynor_getBlock- Get block by hashsynor_getDAGInfo- Get DAG structure infosynor_getChainInfo- Get chain statistics
Transaction Methods
synor_sendRawTransaction- Submit signed transactionsynor_getTransaction- Get transaction by IDsynor_getMempool- Get mempool transactionssynor_estimateFee- Estimate transaction fee
Address Methods
synor_getBalance- Get address balancesynor_getUtxos- Get unspent outputssynor_getAddressTransactions- Get address history
Contract Methods
synor_deployContract- Deploy WASM contractsynor_callContract- Call contract methodsynor_getContractState- Get contract storage
Environment Variables
| Variable | Default | Description |
|---|---|---|
| PORT | 3100 | API gateway port |
| REDIS_URL | redis://localhost:6379 | Redis connection URL |
| RPC_TARGET | http://localhost:16110 | Backend RPC node URL |
| ADMIN_KEY | admin-secret-key | Admin API key |
Development
cd apps/api-gateway
npm install
npm run dev
Production Deployment
The API gateway is designed for horizontal scaling:
- Deploy multiple API gateway instances behind a load balancer
- All instances share the same Redis for rate limiting
- Use Redis Cluster for high availability
- Set unique
ADMIN_KEYper environment
Security Considerations
- Always use HTTPS in production
- Rotate admin keys regularly
- Monitor for abuse patterns
- Set up alerting for high error rates
- Use Redis AUTH in production