---
title: asgi_application (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/asgiapplication-python-agent-api
---

## Syntax

```py
newrelic.agent.asgi_application(application=None, name=None, group=None, framework=None)
```

Monitor web transactions by marking the ASGI application entry point.

## Description

`asgi_application` is a Python decorator used to monitor web transactions by instrumenting the ASGI application entry point. The Python agent automatically supports [most frameworks and servers](https://docs.newrelic.com/docs/agents/python-agent/getting-started/compatibility-requirements-python-agent) that use ASGI. If your framework or web server is not supported, you may need to use this API as part of the [advanced integration process](https://docs.newrelic.com/docs/agents/python-agent/installation-configuration/python-agent-integration#manual-integration).

If you cannot use the `asgi_application` decorator, you can use one of these other call formats:

-   **The wrapper:** The wrapper call is `ASGIApplicationWrapper`. You can use the wrapper in more than one place for distinct ASGI application components that may be stacked. In that case, the first wrapper encountered marks the start of the transaction and the agent determines the target app based on that first wrapper (and ignores subsequent ones).
-   **The path-based wrapper:** The path-based wrapper call is `wrap_asgi_application`. Use this if you could not reference the ASGI object as a variable in the instrumentation scope. This takes the same parameters as the decorator with additional `module` and `object_path` parameters.

For an explanation of the reasons to use the decorator versus the wrappers, see [Different call formats](https://docs.newrelic.com/docs/python-agent-api-different-call-forms).

## Parameters

### Decorator parameters

```py
newrelic.agent.asgi_application(application=None, name=None, group=None, framework=None)
```

The `asgi_application` decorator uses these parameters:

| Parameter                                       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `application` _string_, or _application object_ | Optional. The application name to associate with this data. Default is `None`. If left without a value, the agent uses the application name [specified in the agent configuration](https://docs.newrelic.com/docs/agents/python-agent/installation-configuration/python-agent-configuration#app_name). If a string is provided, it must be the exact application name and cannot be a list of application names. For more on generating an application object, see the [`application`](https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/application) method. |
| `name` _string_                                 | Optional, rarely used. Sets a transaction name for all requests captured via the ASGI entry point. Generally not used, because you usually would not want all transactions to have the same name (see also [Metric grouping issue](https://docs.newrelic.com/docs/agents/manage-apm-agents/troubleshooting/metric-grouping-issues)).                                                                                                                                                                                                                                            |
| `group` _string_                                | Optional, rarely used. The `group` represents the naming structure for the `name` parameter. Setting this creates a [transaction type subcategory](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page#tx_functions). Similar to `name`, this should be rarely used because you usually do not want the entire application to report as one transaction name or category.                                                                                                                                                                         |
| `framework` _tuple_                             | Optional. A tuple with two strings representing the name of the framework and the version number. For example: `('Flask', '0.12.2')`                                                                                                                                                                                                                                                                                                                                                                                                                                            |

### Wrapper parameters

```py
newrelic.agent.ASGIApplicationWrapper(wrapped, application=None, name=None, group=None, framework=None)
```

This takes all of the [parameters](#decorator-parameters) required by `asgi_application` in addition to a `wrapped` parameter:

| Parameter          | Description                                  |
| ------------------ | -------------------------------------------- |
| `wrapped` _object_ | **Required**. The ASGI object to be wrapped. |

### Path-based wrapper parameters [#path-based-parameters]

```py
newrelic.agent.wrap_asgi_application(module, object_path, application=None, name=None, group=None, framework=None)
```

In addition to the [parameters](#decorator-parameters) required by `asgi_application`, this call requires additional `module` and `object_path` parameters:

| Parameter                     | Description                                                                                    |
| ----------------------------- | ---------------------------------------------------------------------------------------------- |
| `module` _object_ or _string_ | **Required**. The module containing the ASGI object.                                           |
| `object_path` _string_        | **Required**. Represents the class method or method for finding the ASGI object within a file. |

## Examples

### asgi_application example [#decorator-example]

An example of the decorator being called without the optional `application` parameter:

```py
@newrelic.agent.asgi_application()
async def application(scope, receive, send):
    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [(b"Content-type", b"text/plain")]
        }
    )
    await send(
        {
            "type": "http.response.body",
            "body": b"Hello World!"
        }
    )
```

### ASGIApplicationWrapper example [#wrapper-example]

An example of using the `ASGIApplicationWrapper`, which may be necessary if the `asgi_application` decorator doesn't work:

```py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.asgi import get_asgi_application
application = get_asgi_application()

application = newrelic.agent.ASGIApplicationWrapper(application)
```

### wrap_asgi_application example [#path-based-example]

An example of using the path-based wrapper:

```py
import newrelic.agent
newrelic.agent.initialize('newrelic.ini')

class Application:
   def __init__(self, scope):
      self.scope = scope

   async def __call__(self, receive, send):
      await send({"type": "http.response.start", "status": 200})
      await send({"type": "http.response.body", "body": b"Hello World!"})

newrelic.agent.wrap_asgi_application(__name__, 'Application')
```
