CORS configuration reference
This page lists every field and option. The overview shows what it does and a first example.
CORS controls which browser origins can call your API. Set it inside the cors: block under apiServer:. If there is no cors: block, kdeps accepts all origins, with credentials enabled by default.
Basic configuration
# workflow.yaml
deploy:
apiServer:
hostIp: "127.0.0.1"
portNum: 16395
routes:
- path: /api/v1/chat
methods: [POST]
cors:
allowOrigins:
- http://localhost:16395
- https://myapp.com
allowMethods:
- GET
- POST
- OPTIONS
allowHeaders:
- Content-Type
- Authorization
allowCredentials: true
maxAge: "24h"Configuration fields
| Field | Type | Description |
|---|---|---|
allowOrigins | array | List of allowed origin domains. Use "*" for all origins (default: ["*"]). kdeps smartly echoes the requested origin when "*" is used to support credentials. |
allowMethods | array | List of HTTP methods allowed for CORS requests. Must be one of: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD. Default: all common methods. |
allowHeaders | array | List of request headers allowed in CORS requests (e.g., Content-Type, Authorization). Default: common headers including Content-Type, Authorization, Accept, X-Requested-With. |
exposeHeaders | array | List of response headers exposed to clients (e.g., X-Request-Id). If unset, no headers are exposed beyond defaults. |
allowCredentials | boolean | Allows credentials (e.g., cookies, HTTP authentication) in CORS requests (default: true) |
maxAge | string | Maximum duration for caching CORS preflight responses (e.g., "24h", "12h"). Default: "12h" |
Common scenarios
Default behavior: smart auto-configuration
By default, kdeps enables CORS for all origins, with credentials. This suits local development, where the frontend (e.g. Vite on :5173) runs on a different port than the API (kdeps on :16395).
To do this, kdeps checks if the incoming Origin matches your allowOrigins list. If "*" is present in the list, kdeps echoes the specific Origin back in the Access-Control-Allow-Origin header. It does not send a literal *. Then the browser can accept Access-Control-Allow-Credentials: true.
# Default behavior (no config needed)
cors:
allowOrigins: ["*"]
allowCredentials: trueProduction: specific origins
For production, limit the origins to specific domains:
# workflow.yaml
cors:
allowOrigins:
- https://myapp.com
- https://www.myapp.com
- https://admin.myapp.com
allowMethods:
- GET
- POST
- PUT
- DELETE
allowHeaders:
- Content-Type
- Authorization
- X-Requested-With
exposeHeaders:
- X-Request-Id
- X-Rate-Limit
allowCredentials: true
maxAge: "24h"Multiple environments
You can configure different CORS settings for different environments:
# Development
cors:
allowOrigins:
- http://localhost:16395
allowCredentials: true
# Production
cors:
allowOrigins:
- https://myapp.com
allowCredentials: true
maxAge: "24h"How CORS works
Simple Requests: For simple requests (GET, POST with certain content types), the browser sends the request directly with an
Originheader.Preflight Requests: For complex requests (PUT, DELETE, custom headers), the browser first sends an OPTIONS request (preflight). This request checks if the server accepts the real request.
Response Headers: The server responds with CORS headers:
Access-Control-Allow-Origin: the origins that the server acceptsAccess-Control-Allow-Methods: the HTTP methods that the server acceptsAccess-Control-Allow-Headers: the headers that the browser can sendAccess-Control-Expose-Headers: the response headers that the browser can readAccess-Control-Allow-Credentials: if the server accepts credentialsAccess-Control-Max-Age: How long to cache preflight responses
Best practices
Security
- Restrict Origins in Production: Use specific domains in
allowOriginsinstead of"*"to enhance security. - Limit Methods and Headers: Accept only the HTTP methods and headers that your API needs. This makes the attack surface smaller.
- Use HTTPS: Always use HTTPS in production to protect credentials and data in transit.
Performance
- Set Appropriate MaxAge: Set a reasonable
maxAge(e.g.,"12h"or"24h") to balance performance and flexibility for preflight requests. - Minimize Exposed Headers: Expose only headers that clients need.
Compatibility
- Disable Credentials When Possible: Set
allowCredentials: falseif your API does not require cookies or authentication headers to simplify CORS handling. - Handle Preflight Requests: Make sure that your routes support the
OPTIONSmethod for preflight requests.
Troubleshooting
CORS errors in browser
If you see CORS errors in the browser console:
- Verify Origins: Make sure your frontend origin is in the
allowOriginslist. - Check Methods: Make sure that the HTTP method is in
allowMethods. - Check Headers: Verify custom headers are in
allowHeaders. - Credentials Mismatch: If you use credentials, make sure that
allowCredentials: trueis set and the origins are not"*".
Common error messages
- "No 'Access-Control-Allow-Origin' header": Origin not in
allowOrigins. - "Method not allowed": HTTP method not in
allowMethods. - "Header not allowed": Custom header not in
allowHeaders. - "Credentials not allowed": The request uses credentials with a wildcard origin (
"*").
Example: full configuration
# workflow.yaml
apiVersion: kdeps.io/v1
kind: Workflow
metadata:
name: cors-example
version: "1.0.0"
targetActionId: apiHandler
deploy:
apiServer:
hostIp: "127.0.0.1"
portNum: 16395
routes:
- path: /api/v1/data
methods: [GET, POST, PUT, DELETE]
cors:
allowOrigins:
- https://myapp.com
- https://admin.myapp.com
allowMethods:
- GET
- POST
- PUT
- DELETE
- OPTIONS
allowHeaders:
- Content-Type
- Authorization
- X-Requested-With
- X-API-Key
exposeHeaders:
- X-Request-Id
- X-Rate-Limit-Remaining
allowCredentials: true
maxAge: "24h"See also
- Workflow configuration - Full workflow settings reference
- API server settings - Complete API server configuration
- WebServer mode - Serving static files and proxying
