---
title: "Context API"
description: "The `Context` object is the heart of every request in this framework. It acts as a bridge between the incoming HTTP request and the outgoing response, providing you with tools to read environment v..."
last_updated: "2026-07-02T09:15:05.267277+00:00"
canonical_url: "https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/core-concepts/context-api"
---

The `Context` object is the heart of every request in this framework. It acts as a bridge between the incoming HTTP request and the outgoing response, providing you with tools to read environment variables, manage request data, and construct sophisticated HTTP responses.

Whenever a handler function is triggered (such as inside `app.get` or `app.use`), the first argument provided is this `Context` object.

## Core Capabilities

The `Context` object provides the following primary functionalities:

1.  **Response Construction:** Methods like `c.json()`, `c.text()`, `c.html()`, and `c.newResponse()` make it simple to return data with the correct headers and status codes.
2.  **State Management:** Use `c.set()` and `c.get()` to pass data between middleware and your final route handlers.
3.  **Environment Access:** Access platform-specific bindings (like KV namespaces, D1 databases, or environment variables) via `c.env`.
4.  **Request Handling:** Access the request instance via `c.req` to read headers, queries, and body content.

## Step-by-Step Usage

### 1. Responding to Requests
You can respond with various data types using built-in methods.

```typescript
app.get('/hello', (c) => {
  // Returns application/json
  return c.json({ message: 'Hello World' });
});

app.get('/plain', (c) => {
  // Returns text/plain
  return c.text('Hello World');
});
```

### 2. Managing Headers and Status Codes
Before returning your response, you can chain method calls to modify the HTTP response.

```typescript
app.get('/custom', (c) => {
  c.header('X-Custom-Header', 'Value');
  c.status(201);
  return c.text('Created');
});
```

### 3. Sharing Data via Context
Context methods allow you to pass information from middleware into your application logic using keys.

```typescript
// Middleware to set a value
app.use('*', async (c, next) => {
  c.set('user-id', '12345');
  await next();
});

// Accessing that value later
app.get('/profile', (c) => {
  const userId = c.get('user-id');
  return c.text(`User ID is ${userId}`);
});
```

## Key Concepts

| Term | Description |
| :--- | :--- |
| `c.env` | An object containing environment-specific bindings (secrets, KV, etc.). |
| `c.req` | The request object used to inspect incoming parameters, headers, and body. |
| `c.var` | A read-only way to access all stored context variables as a single object. |
| `c.render` | A method to generate HTML using a defined layout renderer. |
| `c.res` | The raw Response object currently being prepared. |

> [!TIP]
> You can extend the `ContextVariableMap` interface in your project to get type safety for your custom keys when using `c.get()` and `c.set()`.

> [!WARNING]
> Once a response has been finalized (e.g., returned from the handler), subsequent attempts to change headers via `c.header()` may lead to unexpected behavior. Set your headers before returning the final response.

> [!NOTE]
> Use `c.res` to inspect the response state, but prefer using the dedicated return methods like `c.json()` or `c.text()` to build your final response object.

## Request Workflow

The standard lifecycle of a request through the Context object follows this pattern:

```mermaid
graph LR
    A[Incoming Request] --> B{Middleware}
    B -->|c.set| C[App Handler]
    C -->|c.header / c.status| D[c.json / c.html]
    D --> E[Outgoing Response]
```

## Related

- [Request Handling](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/core-concepts/request-handling)
- [Response Handling](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/core-concepts/response-handling)


## Sitemap

See the full [sitemap](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/llms.txt) for all pages in this wiki.
