> For the complete documentation index, see [llms.txt](https://docs.sigrex.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sigrex.io/reaction/code-reaction.md).

# Code Reaction

A Code Reaction lets you write custom JavaScript code that runs whenever a Data Webhook receives a request.

It works like a [**Code Strategy**](/startegies/code.md), but instead of running on market ticks, it executes **event‑driven** — triggered by incoming webhook data.\
The webhook’s request data is injected into your code so you can react to it, trigger trading actions, store state, and perform HTTP requests.

***

### 🧩 Request Data Object

For each webhook request, Sigrex makes the incoming request available inside your reaction as a `Request` object:

| Field               | Description                        |
| ------------------- | ---------------------------------- |
| `$.Request.ip`      | Sender’s IP address (if available) |
| `$.Request.headers` | All incoming HTTP headers          |
| `$.Request.data`    | Full payload as a string           |

You can parse `$.Request.data` into JSON if needed:

```js
const payload = JSON.parse($.Request.data);
```

***

{% hint style="info" %}

### Code Reactions share the same execution environment and capabilities as [Code Strategies](/startegies/code.md), extended with the `$.Request` object.

{% endhint %}

***

### 🧠 Example Code Reaction

Here’s a sample that reacts to incoming webhook data and triggers trades:

```js
// Parse the incoming payload
const payload = JSON.parse($.Request.data);

// Load persistent state
const state = await $.Storage.get() || { triggers: 0 };
state.triggers++;

// Logic based on external signal
if (payload.signal === "BUY" && payload.confidence > 0.8) {
    await $.Strategy.action($.Action.LONG);
} else if (payload.signal === "SELL") {
    await $.Strategy.action($.Action.EXIT);
}

// Save state
await $.Storage.set(state);
```

This shows:

* Parsing `$.Request.data`
* Using persistent storage
* Applying custom logic
* Triggering trading actions

***

### 🔍 Typical Use Cases

Code Reactions are ideal for:

* Validating and normalizing external webhook data
* Filtering incoming signals before acting
* Coordinating multiple webhook sources
* Building safety layers for automated bots
* Custom preprocessing for downstream logic

***

### 🚫 Limitations

* No chart or image input
* No AI reasoning
* One execution per webhook event
* Deterministic output only

***

### 🔗 Related Features

* [**Data Webhook**](/reaction/data-webhook.md) – Receive event data
* [**LLM Reaction**](/reaction/llm-reaction.md) – AI‑powered reaction logic
* [**Simple Code Strategy** ](/startegies/code.md)– Scheduled market logic

***

### 🧠 Summary

Code Reactions extend the flexibility of Code Strategies into the **event‑driven world**.\
They let you run custom JavaScript logic whenever external systems send data — enabling highly customizable automated workflows with full control over data, state, and trade actions.


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.sigrex.io/reaction/code-reaction.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
