Skip to main content
SonamuContext is an object containing the information needed during API request processing.

Context Overview

Request Info

HTTP request data Headers, body, query

Reply Object

HTTP response control Status code, headers

User Info

Authenticated user ID, permissions, role

Extensible

Add custom fields Project-specific data

SonamuContext Structure

Basic Structure

Sonamu uses Fastify internally, so request and reply are Fastify objects.

Context Components

1. Request

Contains all information related to the HTTP request.
Key properties:
  • headers: HTTP headers (object)
  • query: URL query parameters (object)
  • body: Request body (POST/PUT etc.)
  • params: URL path parameters
  • ip: Client IP address
  • hostname: Hostname
  • url: Request URL
  • method: HTTP method

2. Reply

Controls the HTTP response.
Key methods:
  • status(code): Set status code
  • header(name, value): Set response header
  • cookie(name, value, options): Set cookie
  • redirect(url): Redirect
  • send(data): Send response (usually automatic)

3. User

Contains authenticated user information.
context.user is set by the authentication middleware. APIs requiring authentication should always check for the existence of context.user.

Practical Usage Examples

Authentication Check

Permission Check

Logging

Custom Headers

IP-based Restriction

Rate Limiting Info

Context Usage Pattern

Cautions

Cautions when using Context: 1. Only accessible within API methods 2. context.user only exists after authentication 3. Same Context maintained during async operations 4. Modify Context carefully 5. Set response headers before data transmission

Common Mistakes

Next Steps

Getting Context

Using Sonamu.getContext()

Custom Context

Extending Context

@api Decorator

API basic usage

Error Handling

API error handling