---
title: "Extends context"
description: "Learn how to extend the context in Elysia using decorate, state, and derive to enhance your web applications."
url: "https://next.elysiajs.com/tutorial/patterns/extends-context/"
---

# Extends context

Elysia provides a context with small utilities to help you get started.

You can extend Elysia’s context with:

1. [Decorate](/essential/handler/#extend-context)
2. [State](/essential/handler/#extend-context)
3. [Derive](/essential/life-cycle/#derive)

## Decorate

**Singleton**, and **immutable** properties that are shared across all requests.

```typescript
import { Elysia } from 'elysia'

class Logger {
    log(value: string) {
        console.log(value)
    }
}

new Elysia()
    .decorate('logger', new Logger())
    .get('/', ({ logger }) => {
        logger.log('hi')

        return 'hi'
    })
```

Decorated value it will be available in the context as a read-only property, see [Decorate](/essential/handler/#extend-context).

## State

A **mutable** reference that is shared across all requests.

```typescript
import { Elysia } from 'elysia'

new Elysia()
  .state('count', 0)
  .get('/', ({ store }) => {
    store.count++

    return store.count
  })
```

State will be available in **context.store** that is shared across every request, see [State](/essential/handler/#extend-context).

The first two requests return `1`, then `2`. Increment `store.count` to update the shared state. Destructuring a number with `({ store: { count } })` copies its current value into a local variable. Incrementing that variable does not change `store.count`.

## Derive

[Decorate](/essential/handler/#extend-context) value is registered as a singleton.

[Derive](/essential/life-cycle/#derive) adds context values **per request**, after validation.

```typescript
import { Elysia } from 'elysia'

new Elysia()
  .derive(({ headers: { authorization } }) => ({
    authorization
  }))
  .get('/', ({ authorization }) => authorization)
```

Any **returned value will be available in context** except status, which will be send to client directly, and abort the subsequent handlers.

`derive` runs before the handler, after input has been validated and converted. Return an object to add values to context, or return `status()` to end the request early.

## Scope

[State](/essential/handler/#extend-context), and [Decorate](/essential/handler/#extend-context) are shared across all requests, and instances.



[Derive](/essential/life-cycle/#derive) values are per request, and have an encapsulation scope (as they’re based on life-cycle event).

If you want to use a derived value from a plugin, you would have to declare a [Scope](/essential/plugin/#scope).

```typescript
import { Elysia } from 'elysia'

const plugin = new Elysia()
  .derive(
    'plugin',
    ({ headers: { authorization } }) => ({
      authorization
    })
  )

new Elysia()
  .use(plugin)
  .get('/', ({ authorization }) => authorization)
  .listen(3000)
```

## Assignment

Let’s try to extend Elysia’s context.
