Enabling Authentication
Authentication is controlled by theTOKEN configuration option. If no token is set, the API is publicly accessible.
When a token is configured, DeepLX will print “Access token is set.” during startup.
How Authentication Works
The authentication middleware (authMiddleware in service/service.go:29-61) validates tokens on all protected endpoints:
/translate(Free API)/v1/translate(Pro API)/v2/translate(Official format compatible API)
Token Validation Process
- If
TOKENis not configured, all requests are allowed - If
TOKENis configured, the middleware checks for the token in two locations:- Query parameter:
?token=xxx - Authorization header:
Authorization: Bearer xxxorAuthorization: DeepL-Auth-Key xxx
- Query parameter:
- If neither matches the configured token, a
401 Unauthorizedresponse is returned
Client Authentication Methods
Method 1: Query Parameter
Append the token as a query parameter to the request URL.Method 2: Bearer Token Header
Include the token in theAuthorization header using the Bearer token format.
Method 3: DeepL-Auth-Key Header
For compatibility with DeepL’s official API format, you can use theDeepL-Auth-Key prefix instead of Bearer.
Authorization Header Format
The middleware supports two header formats:- Bearer token:
Authorization: Bearer <token> - DeepL Auth Key:
Authorization: DeepL-Auth-Key <token>
The header must contain exactly two space-separated parts. The first part must be either
Bearer or DeepL-Auth-Key, followed by the token.Authorization: your-token(missing prefix)Authorization: InvalidPrefix your-token(invalid prefix)Authorization: Bearer token with spaces(too many parts)
Unauthorized Response
If authentication fails, the API returns a401 Unauthorized response:
Security Best Practices
Generating Secure Tokens
Disabling Authentication
To disable authentication, simply don’t set theTOKEN configuration:
