> For the complete documentation index, see [llms.txt](https://docs.stoobly.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stoobly.com/guides/how-to-record-requests/how-to-customize-recordings/customizing-with-lifecycle-hooks.md).

# Customizing with Lifecycle Hooks

## Background

{% content-ref url="/pages/ANLbxIN4T0Q4YF2llfer" %}
[Lifecycle Hooks](/core-concepts/agent/lifecycle-hooks.md)
{% endcontent-ref %}

## Writing a Lifecycle Hooks Script

1. Write a Python script (e.g. lifecycle\_hooks.py)
2. In this script, define the lifecycle events to hook into
3. Define what kind of behavior to execute for each of those events

Below is a sample script that prints some strings during two lifecycle events.

{% hint style="warning" %}
Manipulating the **context** object will affect the behaviour of the request interception.
{% endhint %}

```python
from stoobly_agent.app.proxy.context import InterceptContext
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_before_request(context: InterceptContext):
    print('Before request!')

def handle_before_record(context: RecordContext):
    print('Before record!')
```

* `handle_before_request` is for **before** a request gets sent
* `handle_before_record` is for **before** a request gets recorded

After a request gets intercepted, the string `"Before request!"` will be printed. After the response returns, but before the request gets recorded, `"Before record!"` will be printed.

## Enabling Lifecycle Hook Script Use

Pass the path to the lifecycle hooks scripts using the `--lifecycle-hooks-path` option:

```bash
stoobly-agent run --lifecycle-hooks-path ~/path-to-file/lifecycle_hooks.py
```

## Example

```python
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_before_record(context: RecordContext):
    request = context.flow.request
    respose = context.flow.response
    
    request.headers['Authorization'] = '<REDACTED>'
    request.headers['Cookie'] = ''
    
    response.headers['Set-Cookie'] = ''
```


---

# 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.stoobly.com/guides/how-to-record-requests/how-to-customize-recordings/customizing-with-lifecycle-hooks.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.
