Skip to main content
The Accepts helper enables content negotiation by matching request Accept headers (like Accept, Accept-Language, Accept-Encoding) against your supported options. It returns the best match based on client preferences and quality values.

Import

Basic Usage

Match the Accept header to determine response format:

Parameters

Context
required
The Hono context object
acceptsOptions
required
Configuration object for content negotiation

Options Object

AcceptHeader
required
The header to match against. Common values:
  • 'Accept' - Content type
  • 'Accept-Language' - Language preference
  • 'Accept-Encoding' - Compression method
string[]
required
Array of values your application supports (e.g., ['en', 'ja', 'zh'])
string
required
Default value to return if no match is found or header is missing
function
Custom matching function to override default behavior. Receives parsed accepts array and config.
Returns: string - The best matching supported value or the default

Content Type Negotiation

Match the Accept header for API responses:

Language Negotiation

Automatic language selection based on user preferences:

Compression Negotiation

Select compression method based on client support:

Custom Matching Logic

Provide a custom match function for specialized behavior:

Quality Values

The helper automatically respects quality values (q-values) in Accept headers:

Type Definitions

Default Matching Behavior

The default matcher:
  1. Parses the Accept header into an array of Accept objects
  2. Sorts by quality value (q) in descending order
  3. Finds the first value that matches your supports array
  4. Returns the matched value or the default if no match

Best Practices

Always Provide Default

Always specify a sensible default value for cases where the header is missing or no match is found

Order Matters

List supported values in order of preference when multiple have the same quality value

Respect Standards

Use standard MIME types for Accept header (application/json, not json)

Test Edge Cases

Test with missing headers and wildcard values like */*
When the Accept header is missing or empty, the helper returns the default value immediately without attempting to match.