Consumers & Credentials
此内容尚不支持你的语言。
Consumers represent API clients. Credentials provide authentication for consumers.
Consumer Structure
Section titled “Consumer Structure”interface Consumer { id: string; username?: string; // Unique username customId?: string; // External identifier tags?: string[];}Credential Structure
Section titled “Credential Structure”interface Credential { id: string; consumerId: string; credentialType: string; // "api_key", "jwt", etc. credential: object; // Credential data tags?: string[];}Creating Consumers
Section titled “Creating Consumers”Credential Types
Section titled “Credential Types”Managing Consumers
Section titled “Managing Consumers”List Consumers
Section titled “List Consumers”curl http://localhost:8080/api/consumersGet Consumer by Username
Section titled “Get Consumer by Username”curl http://localhost:8080/api/consumers?username=api-clientUpdate Consumer
Section titled “Update Consumer”curl -X PATCH http://localhost:8080/api/consumers/consumer_abc123 \ -H "Content-Type: application/json" \ -d '{ "tags": ["premium", "production"] }'Delete Consumer
Section titled “Delete Consumer”curl -X DELETE http://localhost:8080/api/consumers/consumer_abc123Managing Credentials
Section titled “Managing Credentials”List Credentials
Section titled “List Credentials”curl http://localhost:8080/api/credentialsGet Credentials by Consumer
Section titled “Get Credentials by Consumer”curl http://localhost:8080/api/credentials?consumerId=consumer_abc123Delete Credential
Section titled “Delete Credential”curl -X DELETE http://localhost:8080/api/credentials/credential_xyz789Using with key-auth Plugin
Section titled “Using with key-auth Plugin”Bind key-auth plugin to a route or service:
curl -X POST http://localhost:8080/api/plugins \ -H "Content-Type: application/json" \ -d '{ "serviceId": "service_abc123", "pluginName": "key-auth", "config": { "required": true } }'Requests must include the API key:
Consumer in Plugin Context
Section titled “Consumer in Plugin Context”After authentication, the consumer is available:
// In plugin codectx.consumer.id; // Consumer IDctx.consumer.username; // Usernamectx.consumer.customId; // External IDctx.consumer.tags; // TagsUse consumer identity for:
- Rate limiting per consumer
- Access control decisions
- Request logging with identity
Consumer-Level Plugins
Section titled “Consumer-Level Plugins”Bind plugins to specific consumers:
curl -X POST http://localhost:8080/api/plugins \ -H "Content-Type: application/json" \ -d '{ "consumerId": "consumer_abc123", "pluginName": "rate-limit", "config": { "limit": 500 } }'Best Practices
Section titled “Best Practices”Next Steps
Section titled “Next Steps”- key-auth Plugin - API key authentication
- rate-limit Plugin - Per-consumer rate limiting
- Plugins - Plugin configuration