> For the complete documentation index, see [llms.txt](https://learn.mechanic.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.mechanic.dev/core/tasks/code.md).

# Code

Write task code in Mechanic's Liquid environment — render actions, logs, and errors in response to events.

A task's code is a [**Liquid**](/platform/liquid.md) template. In the same way that a Shopify storefront might use a Liquid template to receive requests and render HTML, a task uses its Liquid code to receive events, and render a series of JSON objects. These JSON objects define [**actions**](/core/tasks/code/action-objects.md), [**logs**](/core/tasks/code/log-objects.md), and [**errors**](/core/tasks/code/error-objects.md).

{% hint style="info" %}
In Mechanic, actions are performed after their originating task run concludes. Actions are not performed inline during the task's Liquid rendering.

To inspect and respond to the results of an HTTP action, add a task subscription to mechanic/actions/perform, allowing the action to re-invoke the task with the action result data.

Learn more: [Responding to action results](/techniques/responding-to-action-results.md)
{% endhint %}

Task code always has access to a set of [**environment variables**](/core/tasks/code/environment-variables.md), which can be used to make decisions about what JSON objects to render.

A task must purposefully consider its [**preview**](/core/tasks/previews.md), so as to accurately communicate its intent to users and to the Mechanic platform.

{% hint style="info" %}
To find many examples of task code, browse <https://github.com/lightward/mechanic-tasks>.
{% endhint %}

## See also

* [Liquid basics](/platform/liquid/basics.md) — syntax, data types, variables, filters, and control flow
* [Mechanic filters](/platform/liquid/filters.md) — Mechanic-specific filters for data manipulation
* [Mechanic tags](/platform/liquid/tags.md) — `{% raw %}{% action %}{% endraw %}`, `{% raw %}{% log %}{% endraw %}`, `{% raw %}{% error %}{% endraw %}`, and more
* [Mechanic objects](/platform/liquid/objects.md) — event, task, options, cache, and action objects available in your code
* [Writing a high-quality task](/techniques/writing-a-high-quality-task.md) — patterns and practices for reliable Mechanic tasks


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://learn.mechanic.dev/core/tasks/code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
