---
title: "Response"
description: "Handle success, errors, streams, and response types in Eden Treaty."
url: "https://next.elysiajs.com/eden/treaty/response/"
---

# Response

Once the fetch method is called, Eden Treaty returns a `Promise` containing an object with the following properties:

- data - returned value of the response (2xx)
- error - `{ status, value }` for a non-2xx response, with the server’s returned value in `error.value`
- response `Response` - Web Standard Response class
- status `number` - HTTP status code
- headers `Headers` - response headers

Once returned, you must provide error handling to ensure that the response data value is unwrapped; otherwise, the value will be nullable. Elysia provides a `status()` helper to return a status-specific response, and Eden will provide type narrowing for the error value.

```ts
import { Elysia, t } from 'elysia'
import { treaty } from '@elysia/eden'

const app = new Elysia()
    .post('/user', {
        body: t.Object({
            name: t.String()
        })
    }, ({ body: { name }, status }) => {
        if(name === 'Otto') return status(400)

        return name
    })
    .listen(3000)

const api = treaty<typeof app>('localhost:3000')

const submit = async (name: string) => {
    const { data, error } = await api.user.post({
        name
    })

    // type: string | null
    console.log(data)

    if (error)
        switch(error.status) {
            case 400:
                // Error type will be narrow down
                throw error.value

            default:
                throw error.value
        }

    // Once the error is handled, type will be unwrapped
    // type: string
    return data
}
```

By default, Elysia infers `error` and `response` types to TypeScript automatically, and Eden will be providing auto-completion and type narrowing for accurate behavior.

Tip

If the server responds with an HTTP status >= 300, then `data` will be `null`, and `error` will be `{ status, value }`, with the server’s returned value in `error.value`.

Otherwise, the parsed response is passed to `data`. Fetch follows redirects by default, so a redirect may resolve to its destination response first.

## Stream response

Eden will interpret a stream response or [Server-Sent Events](/essential/handler#server-sent-events) as an `AsyncGenerator`, allowing us to use a `for await` loop to consume the stream.

**Stream**

```ts
import { Elysia } from 'elysia'
import { treaty } from '@elysia/eden'

const app = new Elysia()
  .get('/ok', function* () {
    yield 1
    yield 2
    yield 3
  })

const { data, error } = await treaty(app).ok.get()
if (error) throw error

for await (const chunk of data)
  console.log(chunk)
```

**Server-Sent Events**

```ts
import { Elysia, sse } from 'elysia'
import { treaty } from '@elysia/eden'

const app = new Elysia()
  .get('/ok', function* () {
    yield sse({
      event: 'message',
      data: '1'
    })
    yield sse({
      event: 'message',
      data: '2'
    })
    yield sse({
      event: 'end'
    })
  })

const { data, error } = await treaty(app).ok.get()
if (error) throw error

for await (const chunk of data)
  console.log(chunk)
```

## Utility types

Eden Treaty provides utility types `Treaty.Data<T>` and `Treaty.Error<T>` to extract the `data` and `error` types from the response.

```ts
import { Elysia, t } from 'elysia'

import { treaty, type Treaty } from '@elysia/eden'

const app = new Elysia()
  .post('/user', {
    body: t.Object({
      name: t.String()
    })
  }, ({ body: { name }, status }) => {
    if(name === 'Otto') return status(400)

    return name
  })
  .listen(3000)

const api =
  treaty<typeof app>('localhost:3000')

type UserData = Treaty.Data<typeof api.user.post>

// Alternatively, you can also pass a response
const response = await api.user.post({
  name: 'Saltyaom'
})

type UserDataFromResponse = Treaty.Data<typeof response>

type UserError = Treaty.Error<typeof api.user.post>
```
