# Lifecycle Hooks

{% hint style="info" %}
We currently only support lifecycle hook scripts written in Python.
{% endhint %}

## What are Lifecycle Hooks?

A lifecycle hook is a way to add custom functionality during specific events in the life of a Stoobly resource such as a request or a response.

Stoobly's Lifecycle Hooks can be written as Python functions which use the Stoobly Python library to hook into these events and do addtional tasks.

## Why use Lifecycle Hooks?

These hooks enable you to add custom functionality with your own code at different stages.

Some examples include:

* Rewriting a header's value before or after its request gets sent to not store sensitive data
* Adding, removing or modifying data such as in headers, query parameters, bodies, etc.
* Adding extra debug information or publishing metrics

## What are the Different Lifecycle Hook Events Supported?

<details>

<summary>handle_before_request</summary>

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

def handle_before_request(context: InterceptContext):
    pass
```

</details>

<details>

<summary>handle_before_record</summary>

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

def handle_before_record(context: RecordContext):
    pass
```

</details>

<details>

<summary>handle_before_mock</summary>

```python
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_before_mock(context: MockContext):
    pass
```

</details>

<details>

<summary>handle_before_test</summary>

```python
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_mock(context: TestContext):
    pass
```

</details>

<details>

<summary>handle_after_record</summary>

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

def handle_after_record(context: RecordContext):
    pass
```

</details>

<details>

<summary>handle_after_mock</summary>

```python
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_after_mock(context: MockContext):
    pass
```

</details>

<details>

<summary>handle_after_test</summary>

```python
from stoobly_agent.app.proxy.test.context import TestContext

def handle_after_test(context: TestContext):
    pass
```

</details>

<details>

<summary>handle_before_replay</summary>

```python
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_before_replay(context: ReplayContext):
    pass
```

</details>

<details>

<summary>handle_after_replay</summary>

```python
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_after_replay(context: ReplayContext):
    pass
```

</details>

<details>

<summary>handle_before_response</summary>

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

def handle_before_response(context: InterceptContext):
    pass
```

</details>

<details>

<summary>handle_before_request_component_create</summary>

```python
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_create(context: TestContext):
    pass
```

</details>

<details>

<summary>handle_before_request_component_delete</summary>

```python
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_delete(context: TestContext):
    pass
```

</details>

For the full list, see [this link](https://github.com/Stoobly/stoobly-agent/blob/master/stoobly_agent/config/constants/lifecycle_hooks.py) to the Stoobly Python definitions of the events available. All of the strings on the right side of the variable assignment are also the function names available in lifecycle hook scripts.

## Types of Contexts

There are several types of contexts available for your scripts. The parameter type will change depending on what type of hook you are using. To see the fields and methods available, view the full definition as well as any parent classes they might be inherited from.

#### RecordContext

For record events use the `RecordContext`. The full definition can [be found here](https://github.com/Stoobly/stoobly-agent/blob/master/stoobly_agent/app/proxy/record/context.py).

To import it, add this to the top of your script:

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

#### ReplayContext

For replay events use the `ReplayContext`. The full definition can [be found here](https://github.com/Stoobly/stoobly-agent/blob/master/stoobly_agent/app/proxy/replay/context.py).

To import it, add this to the top of your script:

```python
from stoobly_agent.app.proxy.replay.context import ReplayContext
```

#### MockContext

For mock events use the `MockContext`. The full definition can [be found here](https://github.com/Stoobly/stoobly-agent/blob/master/stoobly_agent/app/proxy/mock/context.py).

To import it, add this to the top of your script:

```python
from stoobly_agent.app.proxy.mock.context import MockContext
```

## Enabling Lifecycle Hook Scripts Use

Once your script is written, it can now be used by the Agent. Lifecycle hooks are supported for the following commands:

* `stoobly-agent run`
* `stoobly-agent request replay`
* `stoobly-agent scenario replay`
* `stoobly-agent endpoint import`

## Next Steps

Next see our guide on how to create your own lifecycle hooks and how to use them:

{% content-ref url="../../guides/how-to-record-requests/how-to-customize-recordings" %}
[how-to-customize-recordings](https://docs.stoobly.com/guides/how-to-record-requests/how-to-customize-recordings)
{% endcontent-ref %}
