Python ASGI

This module is made for Python Asynchronous Server Gateway Interface (ASGI) application.

Supported version

DataDome supports Python versions until they reach their End of Life date.
You can find the status of Python versions on the official website.

This module is compatible with ASGI application version 3.0 and above.

Prerequisites

Installation

The module can be installed with pip:

pip install datadome-asgi

Usage

The Python ASGI module is built as an ASGI middleware.

from datadome_asgi import DataDomeMiddleware

# Create FastAPI application
app = FastAPI()

# Bind the ASGI middleware on the FastAPI application
app.add_middleware(
    DataDomeMiddleware,
    server_side_key="YOUR_SERVER_SIDE_KEY",
)

Settings

SettingDescriptionDefault value
server_side_keyYour DataDome server-side key.
appThe ASGI application the middleware needs to be attached to.
url_pattern_exclusionRegex to match to exclude requests from being processed with the Protection API.r"\.(avi|avif|bmp|css|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|json|less|map|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|otf|png|svg|svgz|swf|tiff|ttf|wav|webm|webp|woff|woff2|xml|zip)$"
url_pattern_inclusionRegex to match to process the request with the Protection API.""
endpoint_hostHost of the Protection API.api.datadome.co
timeoutThe timeout limit for requests sent to the Protection API, in milliseconds.150
enable_mcp_supportBoolean to enable Model Context Protocol support.false
mcp_endpointMCP endpoint on which the information should be extracted.
You can specify several endpoints separated with commas.
mcp

Here is an example of configuration with default values that you can modify according to your needs:

from datadome_asgi import DataDomeMiddleware

# Create FastAPI application
app = FastAPI()

# Bind the ASGI middleware on the FastAPI application
app.add_middleware(
    DataDomeMiddleware,
    server_side_key="YOUR_SERVER_SIDE_KEY",
    urlPatternExclusion=r"\.(avi|avif|bmp|css|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|json|less|map|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|otf|png|svg|svgz|swf|tiff|ttf|wav|webm|webp|woff|woff2|xml|zip)$",
    urlPatternInclusion="",
    endpoint_host="api.datadome.co",
    timeout=150,
    enable_mcp_support=False,
    mcp_endpoint="mcp",
)

Advanced configuration

Enable MCP support

You can enable support for the Model Context Protocol and extract information from the request body.

Set the enable_mcp_support setting to true.

Once enabled, requests targeting an MCP endpoint will be analyzed to extract MCP properties according to the official specification:

  • mcp-session-id and mcp-protocol-version headers
  • jsonRpcVersion, jsonRpcRequestId, and mcpMethod request body fields
  • mcpParamsClientInfoName and mcpParamsClientInfoVersion request body fields when the method is initialize
  • mcpParamsToolName request body field when the method is tools/call

Starting from version 1.2.0, you can customize the MCP endpoint by defining the mcp_endpoint setting.



Did this page help you?