> For the complete documentation index, see [llms.txt](https://julien-sarazin.gitbook.io/idylle/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://julien-sarazin.gitbook.io/idylle/api/context.md).

# Context

{% hint style="warning" %}
A part of the context method is linked to the default [ResponseHandler](/idylle/core/concepts/responsehandler.md) provided by Idylle. Specific method are design to adapt HTTP code when responding to an HTTP Request. For a better understanding feel free to check the [HTTP Code Meaning](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).
{% endhint %}

## Properties

### data : Object { String: Any }

### params : Object { String: String }

### token: String

### user: Object { String: Any }

### session: Object { String: Any }

### files: Array\<File>

### args: Object { String : Any }

### criteria: Criteria

{% hint style="info" %}
Note the properties will depends on the `CriteriaBuilder` used by the Action. It can be overridden globally during the [initialization phase](/idylle/core/the-flows.md#customizing-the-flow) or for a specific action by overriding the criteriaBuilder property.
{% endhint %}

## Methods

### .error(code, message, ...args) -> Promise (rejected)

`.error(code, message, ...args) -> Promise (rejected)` create a rejection with a [ContextError.](/idylle/api/contexterror.md)

```javascript
...
execute: context => {
    return User
        .findById(context.params.id)
        .then(u => u || context.error(404, 'user_not_found', { id: context.params.id }))
}
...
```

### .ok(resource) -> resource

`context.ok(resource) -> resource` changes the state of the context to tell the default `ResponseHandler` that the response should be a **200 OK**.&#x20;

```javascript
...
        execute: context => {
            return getCompetitionDetails()
                .then(c => c || context.error(400, 'invalid competition'))
                .then(context.ok);
...
```

### .created(resource) -> resource

`context.created(resource) -> resource` changes the state of the context to tell the default `ResponseHandler` that the response should be a **201 Created** and let the response body empty.

{% hint style="info" %}
Note that if the provided resource has a property `uri` it will be written in the `location` response header.
{% endhint %}

```javascript
...
        execute: context => {
            return getCompetitionDetails()
                .then(c => c || context.error(400, 'invalid competition'))
                .then(context.created);
...
```

### .partial(resource) -> resource

`context.partial(resource) -> resource` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **206 Partial**.&#x20;

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```

### .redirect(code=302, url) -> void

`context.redirect(code, url) -> void` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **301** **Moved Permanently**. Generally used when a deprecated method is still called from an old client.

```javascript
... 
        execute: context => {
            return context.redirect(null, '/competitions/list');
...
```

### .stream(path | buffer) -> resource

`context.stream(path | buffer) -> void` changes the state of the context to tell the default  `ResponseHandler` that a `File` or `Buffer` has to be downloaded from the given path.&#x20;

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```

### .noContent(resource) -> resource

`context.noContent(resource) -> resource` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **204 No Content** and let the response body empty.

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://julien-sarazin.gitbook.io/idylle/api/context.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
