API Gateway Setup (Tyk) for Web Application
Tyk is an open-source API Gateway written in Go, an alternative to Kong. Includes Dashboard, Developer Portal, and built-in support for GraphQL and gRPC. Community Edition is fully functional without licensing restrictions on the number of APIs.
Tyk Components
- Tyk Gateway — the proxy server itself
- Tyk Dashboard — web UI for API management
- Tyk Pump — analytics aggregation to Redis/Elasticsearch/MongoDB
- Tyk Developer Portal — portal for external developers
Docker Compose Deployment
version: '3.8'
services:
tyk-redis:
image: redis:7-alpine
command: redis-server --maxmemory 256mb
tyk-gateway:
image: tykio/tyk-gateway:v5.3
volumes:
- ./tyk.conf:/opt/tyk-gateway/tyk.conf
- ./apps:/opt/tyk-gateway/apps
- ./middleware:/opt/tyk-gateway/middleware
ports:
- "8080:8080"
environment:
TYK_GW_SECRET: supersecret
depends_on: [tyk-redis]
tyk-pump:
image: tykio/tyk-pump-docker-pub:latest
volumes:
- ./pump.conf:/opt/tyk-pump/pump.conf
depends_on: [tyk-redis]
// tyk.conf
{
"listen_port": 8080,
"secret": "supersecret",
"template_path": "/opt/tyk-gateway/templates",
"use_db_app_configs": false,
"app_path": "/opt/tyk-gateway/apps/",
"storage": {
"type": "redis",
"host": "tyk-redis",
"port": 6379
},
"enable_analytics": true,
"analytics_config": {
"type": "rpc"
},
"health_check": {
"enable_health_checks": true,
"health_check_value_timeouts": 60
},
"optimisations_use_async_session_write": true
}
API Definition File
Each API is described by a JSON file in the apps/ directory:
// apps/users-api.json
{
"name": "Users API",
"api_id": "users-api",
"slug": "users-api",
"org_id": "default",
"use_keyless": false,
"auth": {
"auth_header_name": "Authorization",
"use_param": false
},
"definition": {
"location": "header",
"key": "x-api-version"
},
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
"use_extended_paths": true,
"extended_paths": {
"rate_limit": [
{
"path": "/",
"method": "GET",
"rate": 100,
"per": 60
}
],
"transform": [
{
"path": "/users",
"method": "POST",
"template_data": {
"template_mode": "blob",
"blob": "eyJlbWFpbCI6ICJ7e3JlcXVlc3RfZGF0YS5lbWFpbH19In0="
}
}
]
}
}
}
},
"proxy": {
"listen_path": "/api/v1/users/",
"target_url": "http://users-service:3000",
"strip_listen_path": true
},
"rate_limit_and_quota_middleware": true
}
Key Management via API
# Create API key
curl -X POST http://localhost:8080/tyk/keys/create \
-H "x-tyk-authorization: supersecret" \
-H "Content-Type: application/json" \
-d '{
"alias": "mobile-app",
"rate": 1000,
"per": 60,
"quota_max": 50000,
"quota_renewal_rate": 86400,
"access_rights": {
"users-api": {
"api_name": "Users API",
"api_id": "users-api",
"versions": ["Default"]
}
}
}'
# Response contains key_id — share with developer
JavaScript Middleware
Tyk supports custom middleware in JavaScript (V8 engine):
// middleware/add-tenant-id.js
var addTenantId = new TykJS.TykMiddleware.NewMiddleware({})
addTenantId.NewProcessRequest(function(request, session, spec) {
var tenantId = session.meta_data['tenant_id']
request.SetHeaders['X-Tenant-ID'] = tenantId
return TykJS.TykMiddleware.ReturnData(request, {})
})
// In API definition
"custom_middleware": {
"pre": [
{
"name": "addTenantId",
"path": "middleware/add-tenant-id.js",
"require_session": true
}
]
}
GraphQL Proxying
// apps/graphql-api.json
{
"name": "GraphQL API",
"graphql": {
"enabled": true,
"execution_mode": "proxyOnly",
"schema": "type Query { user(id: ID!): User }\ntype User { id: ID name: String }",
"version": "2",
"proxy": {
"auth_headers": {
"Authorization": "Bearer internal-token"
}
},
"type_field_configurations": [],
"playground": {
"enabled": true,
"path": "/playground"
}
},
"proxy": {
"target_url": "http://graphql-service:4000/graphql"
}
}
Analytics via Tyk Pump
// pump.conf
{
"pumps": {
"elasticsearch": {
"type": "elasticsearch",
"meta": {
"index_name": "tyk_analytics",
"elasticsearch_url": "http://elasticsearch:9200"
}
},
"prometheus": {
"type": "prometheus",
"meta": {
"listen_address": "0.0.0.0:9090"
}
}
},
"uptime_pump_config": {
"collection_name": "tyk_uptime_analytics",
"database_url": "mongodb://mongo:27017/tyk_analytics"
},
"analytics_storage_type": "redis",
"analytics_storage_config": {
"host": "tyk-redis"
}
}
Differences from Kong
| Feature | Kong | Tyk |
|---|---|---|
| Language | Lua/Go | Go |
| Middleware | Lua plugins | JS/Go plugins |
| GraphQL | Plugin | Built-in |
| Dashboard | Paid (Kong Manager EE) | Free |
| Configuration | API/DB | Files/API/DB |
| Developer Portal | Not in OSS | Included |
Timeline
Installing Tyk Gateway with Dashboard and basic API configuration — 2 business days. Full setup with middleware, analytics, and Developer Portal — 4–5 days.







