Defining API Routes and Endpoints

1. Creating Route Definitions

FieldDescriptionExample
nameRoute identifierget-order
pathsMatch patterns/orders/:id
methodsAllowed HTTP verbs[GET, HEAD]
hostsHost header matchapi.example.com
serviceUpstream targetorders-svc
strip_pathRemove prefixtrue

Example: Kong route

routes:
  - name: get-order
    service: orders-svc
    paths: ["/v1/orders"]
    methods: [GET, POST]
    hosts: ["api.example.com"]
    strip_path: true
    preserve_host: false
    https_redirect_status_code: 301

2. Configuring Method-Specific Routes

MethodUseIdempotent
GETRead resourceYes
POSTCreate / RPCNo
PUTReplaceYes
PATCHPartial updateNo (usually)
DELETERemoveYes
OPTIONSCORS preflightYes

3. Using Path Parameters

SyntaxMatchesCaptures
/users/:id/users/42id=42
/users/{id}OpenAPI styleid
/users/:id/posts/:pidNestedid, pid
/files/*pathGreedy capturepath=a/b/c

4. Using Query Parameters

Example: Query routing

routes:
  - paths: ["/search"]
    methods: [GET]
    headers: {}
    query_string:
      version: ["2"]   # match ?version=2
    service: search-v2
OperatorMatch
exact?env=prod
regex^v\d+$
presentKey exists, any value
absentKey must not exist

5. Using Regular Expression Patterns

PatternMatches
~ ^/api/v\d+/Versioned APIs
~* \.(jpg|png)$Image extensions (case-insensitive)
(?<ver>v\d+)Named capture group
^/users/(\d+)$Numeric ID only
Warning: Regex routes are slower than prefix matches. Use sparingly and benchmark.

6. Setting Up Wildcard Routes (*, **)

PatternMeaning
/api/*Single segment wildcard
/api/**Multi-segment recursive
*.example.comSubdomain wildcard
/static/*filepathCapture remainder

7. Configuring Route Priority and Order

RuleOrder
Exact match1 (highest)
Longest prefix2
Regex3
Wildcard4
Catch-all /5 (lowest)
Explicit priority fieldOverrides above

8. Setting Up Catch-All Routes

Example: 404 fallback

routes:
  - name: not-found
    paths: ["/"]
    priority: 0
    service: error-service
    plugins:
      - name: request-termination
        config:
          status_code: 404
          message: "Endpoint not found"
Use CasePattern
Default 404/ last priority
Maintenance pageMatch all → static response
SPA fallback/*index.html

9. Defining Route Descriptions and Tags

FieldPurpose
descriptionFree-text doc
tagsGroup/filter (env:prod)
x-internalHide from public docs
deprecatedSurface sunset warnings

10. Configuring Route-Level Timeouts

Example: Per-route timeout override

routes:
  - name: long-report
    paths: ["/reports/generate"]
    service: reports
    connect_timeout: 5000
    read_timeout: 300000    # 5 min for long jobs
    write_timeout: 60000
SettingScopeWins
Service timeoutAll routesDefault
Route timeoutOne routeOverrides service
Plugin timeoutSpecific pluginLocal only