{"id":16026964,"url":"https://github.com/moesif/moesifasgi","last_synced_at":"2025-11-11T20:04:54.366Z","repository":{"id":37794412,"uuid":"400666016","full_name":"Moesif/moesifasgi","owner":"Moesif","description":"Moesif middleware for Python ASGI-based frameworks, for API Monitoring, Analytics, and Monetization.","archived":false,"fork":false,"pushed_at":"2024-12-04T23:20:36.000Z","size":171,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-16T16:44:10.027Z","etag":null,"topics":["analytics","api-analytics","api-logs","api-management","api-monetization","api-monitoring","asgi","asgi-framework","asgi-middleware","fastapi","logger","logging","monitoring","observability","python","usage-based-billing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Moesif.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-28T00:30:01.000Z","updated_at":"2024-12-04T23:11:47.000Z","dependencies_parsed_at":"2024-05-09T00:31:49.397Z","dependency_job_id":"1b558f4e-50cb-43b4-a6bd-62edd65b8410","html_url":"https://github.com/Moesif/moesifasgi","commit_stats":{"total_commits":42,"total_committers":5,"mean_commits":8.4,"dds":0.5476190476190477,"last_synced_commit":"81fe376c6836c994ea1e5242a3b9da7c2531c75e"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifasgi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifasgi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifasgi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifasgi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Moesif","download_url":"https://codeload.github.com/Moesif/moesifasgi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244151158,"owners_count":20406731,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","api-analytics","api-logs","api-management","api-monetization","api-monitoring","asgi","asgi-framework","asgi-middleware","fastapi","logger","logging","monitoring","observability","python","usage-based-billing"],"created_at":"2024-10-08T20:04:21.399Z","updated_at":"2025-11-11T20:04:54.290Z","avatar_url":"https://github.com/Moesif.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moesif Middleware for Python ASGI-based Frameworks\n\n[![Built For][ico-built-for]][link-built-for]\n[![Latest Version][ico-version]][link-package]\n[![Language Versions][ico-language]][link-language]\n[![Software License][ico-license]][link-license]\n[![Source Code][ico-source]][link-source]\n\nWith Moesif middleware for Python ASGI-based frameworks, you can automatically log API calls \nand send them to [Moesif](https://www.moesif.com) for API analytics and monitoring.\n\n\u003e If you're new to Moesif, see [our Getting Started](https://www.moesif.com/docs/) resources to quickly get up and running.\n\n## Overview\n\nThis middleware allows you to easily integrate Moesif's API analytics and \nAPI monetization service with APIs built on Python ASGI-based (Asynchronous Server Gateway Interface) frameworks including [FastAPI](https://fastapi.tiangolo.com/) and [Starlette](https://www.starlette.io/).\n\n[ASGI](https://asgi.readthedocs.io/en/latest/)\nis a spiritual successor to WSGI (Web Server Gateway Interface) enabling async-capable applications.\n\n## Prerequisites\nBefore using this middleware, make sure you have the following:\n\n- [An active Moesif account](https://moesif.com/wrap)\n- [A Moesif Application ID](#get-your-moesif-application-id)\n\n### Get Your Moesif Application ID\nAfter you log into [Moesif Portal](https://www.moesif.com/wrap), you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:\n\n1. Select the account icon to bring up the settings menu.\n2. Select **Installation** or **API Keys**.\n3. Copy your Moesif Application ID from the **Collector Application ID** field.\n\n\u003cimg class=\"lazyload blur-up\" src=\"images/app_id.png\" width=\"700\" alt=\"Accessing the settings menu in Moesif Portal\"\u003e\n\n## Install the Middleware\nInstall with `pip` using the following command:\n\n```shell\npip install moesifasgi\n```\n\n## Configure the Middleware\nSee the available [configuration options](#configuration-options) to learn how to configure the middleware for your use case. \n\n## How to Use\n\n### FastAPI\n\nSimply add `MoesifMiddleware` to a FastAPI application using the `add_middleware` method:\n\n```python\nfrom moesifasgi import MoesifMiddleware\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_APPLICATION_ID',\n    'LOG_BODY': True,\n    # ... For other options see below.\n}\n\napp = FastAPI()\n\napp.add_middleware(MoesifMiddleware, settings=moesif_settings)\n```\n\nReplace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).\n\n### Other ASGI Frameworks\n\nIf you are using a framework that is built on top of ASGI, it should work just by adding the Moesif middleware.\nPlease read the documentation for your specific framework on how to add middlewares.\n\n### Optional: Capturing Outgoing API Calls\nIn addition to your own APIs, you can also start capturing calls out to third party services through by setting the `CAPTURE_OUTGOING_REQUESTS` option:\n\n```python\nfrom moesifasgi import MoesifMiddleware\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_APPLICATION_ID',\n    'LOG_BODY': True,\n    'CAPTURE_OUTGOING_REQUESTS': False,\n}\n\napp = FastAPI()\n\napp.add_middleware(MoesifMiddleware, settings=moesif_settings)\n```\n\nFor configuration options specific to capturing outgoing API calls, see [Options For Outgoing API Calls](#options-for-outgoing-api-calls).\n\n## Troubleshoot\nFor a general troubleshooting guide that can help you solve common problems, see [Server Troubleshooting Guide](https://www.moesif.com/docs/troubleshooting/server-troubleshooting-guide/).\n\nOther troubleshooting supports:\n\n- [FAQ](https://www.moesif.com/docs/faq/)\n- [Moesif support email](mailto:support@moesif.com)\n\n## Repository Structure\n\n```\n.\n├── BUILDING.md\n├── examples/\n├── images/\n├── LICENSE\n├── MANIFEST.in\n├── moesifasgi/\n├── README.md\n├── requirements.txt\n├── setup.cfg\n└── setup.py\n```\n\n## Configuration Options\nThe following sections describe the available configuration options for this middleware. You can set these options in a Python dictionary and then pass that as a parameter to `add_middleware` when you add this middleware. See [the sample FastAPI code](https://github.com/Moesif/moesifasgi/blob/1c43c02d2e90b392980371bd4382a609fbdd7f06/examples/fastapi/main.py#L218-L233) for an example.\n\nSome configuration options use request and response as input arguments. These correspond to the [`Requests`](https://www.starlette.io/requests/) and [`Responses`](https://www.starlette.io/responses/) objects respectively.\n\n#### `APPLICATION_ID` (Required)\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nA string that [identifies your application in Moesif](#get-your-moesif-application-id).\n\n#### `SKIP`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(request, response)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a request and a response,\nand returns `True` if you want to skip this particular event.\n\n#### `IDENTIFY_USER`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(request, response)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eString\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional, but highly recommended.\n\nA function that takes a request and a response, and returns a string that represents the user ID used by your system. \n\nMoesif identifies users automatically. However, due to the differences arising from different frameworks and implementations, provide this function to ensure user identification properly.\n\n#### `IDENTIFY_COMPANY`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(request, response)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional. \n\nA function that takes a request and response, and returns a string that represents the company ID for this event.\n\n#### `GET_METADATA`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(request, response)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Dictionary\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nThis function allows you\nto add custom metadata that Moesif can associate with the event. The metadata must be a simple Python dictionary that can be converted to JSON. \n\nFor example, you may want to save a virtual machine instance ID, a trace ID, or a resource ID with the request.\n\n#### `GET_SESSION_TOKEN`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(request, response)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a request and response, and returns a string that represents the session token for this event. \n\nSimilar to users and companies, Moesif tries to retrieve session tokens automatically. But if it doesn't work for your service, provide this function to help identify sessions.\n\n#### `MASK_EVENT_MODEL`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(EventModel)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eEventModel\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes the final Moesif event model and returns an event model with desired data removed. \n\nThe return value must be a valid eventt model required by Moesif data ingestion API. For more information about the `EventModel` object, see the [Moesif Python API documentation](https://www.moesif.com/docs/api?python).\n\n#### `DEBUG`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nSet to `True` to print debug logs if you're having integration issues.\n\n#### `LOG_BODY`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eTrue\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nWhether to log request and response body to Moesif.\n\n#### `BATCH_SIZE`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e100\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nAn optional field name that specifies the maximum batch size when sending to Moesif.\n\n#### `EVENT_BATCH_TIMEOUT`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e1\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nMaximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue.\n\n#### `EVENT_QUEUE_SIZE`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e1000000\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nThe maximum number of event objects queued in memory pending upload to Moesif. For a full queue, additional calls to `MoesifMiddleware` returns immediately without logging the event. Therefore, set this option based on the event size and memory capacity you expect.\n\n#### `AUTHORIZATION_HEADER_NAME`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default \n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eauthorization\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA request header field name used to identify the User in Moesif. It also supports a comma separated string. Moesif checks headers in order like `\"X-Api-Key,Authorization\"`.\n\n#### `AUTHORIZATION_USER_ID_FIELD`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default \n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003esub\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA field name used to parse the user from authorization header in Moesif.\n\n#### `BASE_URI`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA local proxy hostname when sending traffic through secure proxy. Remember to set this field when using secure proxy. For more information, see [Secure Proxy documentation.](https://www.moesif.com/docs/platform/secure-proxy/#2-configure-moesif-sdk).\n\n### Options For Outgoing API Calls\n\nThe following options apply to outgoing API calls. These are calls you initiate using the Python [Requests](http://docs.python-requests.org/en/master/) library to third parties like Stripe or to your own services.\n\nSeveral options use request and response as input arguments. These correspond to the [Requests](http://docs.python-requests.org/en/master/api/) library's request or response objects.\n\nIf you are not using ASGI, you can import [`moesifpythonrequest`](https://github.com/Moesif/moesifpythonrequest) directly.\n\n#### `CAPTURE_OUTGOING_REQUESTS` (Required)\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default \n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eFalse\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nSet to `True` to capture all outgoing API calls.\n\n##### `GET_METADATA_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Dictionary\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that enables you to return custom metadata associated with the logged API calls.\n\nTakes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response objects as arguments. \n\nWe recommend that you implement a function that\nreturns a dictionary containing your custom metadata. The dictionary must be a valid one that can be encoded into JSON. For example, you may want to save a virtual machine instance ID, a trace ID, or a resource ID with the request.\n\n##### `SKIP_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response objects,\nand returns `True` if you want to skip this particular event.\n\n##### `IDENTIFY_USER_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional, but highly recommended.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that represents the user ID used by your system. \n\nWhile Moesif tries to identify users automatically, different frameworks and your implementation might vary. So we highly recommend that you accurately provide a \nuser ID using this function.\n\n##### `IDENTIFY_COMPANY_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that represents the company ID for this event.\n\n##### `GET_SESSION_TOKEN_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that corresponds to the session token for this event. \n\nSimilar to [user IDs](#identify_user_outgoing), Moesif tries to get the session token automatically. However, if you setup differs from the standard, this function can help tying up events together and help you replay the events.\n\n##### `LOG_BODY_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eTrue\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nSet to `False` to remove logging request and response body.\n\n## Examples\nSee the example FastAPI app in the `examples/` folder of this repository that uses this middleware.\n\nHere's another sample FastAPI app:\n\n```python\n# identify user not using async mode\ndef identify_user(request, response):\n    return \"12345\"\n\n# identify company not using async mode\ndef identify_company(request, response):\n    return \"67890\"\n\n# get metadata not using async mode\ndef get_metadata(request, response):\n    return {\n        'datacenter': 'westus',\n        'deployment_version': 'v1.2.3',\n    }\n\n# should skip check not using async mode\ndef should_skip(request, response):\n    return \"health/probe\" in request.url._url\n\n# mask event not using async mode\ndef mask_event(eventmodel):\n    # Your custom code to change or remove any sensitive fields\n    if 'password' in eventmodel.response.body:\n        eventmodel.response.body['password'] = None\n    return eventmodel\n\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n    'DEBUG': False,\n    'IDENTIFY_USER': identify_user,\n    'IDENTIFY_COMPANY': identify_company,\n    'GET_METADATA': get_metadata,\n    'SKIP': should_skip,\n    'MASK_EVENT_MODEL': mask_event,\n    'CAPTURE_OUTGOING_REQUESTS': False,\n}\n\napp = FastAPI()\n\napp.add_middleware(MoesifMiddleware, settings=moesif_settings)\n```\n\nYou can use OAuth2 in your FastAPI app with this middleware. For more information, see [OAuth2 with Password (and hashing), Bearer with JWT tokens](https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/).\n\n## Tested versions\n\nMoesif has validated this middleware against the following frameworks and framework versions:\n\n| Framework | Version |\n|---------|-------------------|\n| fastapi | \u003e 0.51.0 |\n| fastapi | \u003e 0.78.0 |\n| fastapi | \u003e 0.108.0 |\n| fastapi | \u003e 0.115.5 |\n\n## Examples\n- [View example app with FastAPI](https://github.com/Moesif/moesifasgi/tree/master/examples/fastapi).\n\n\n## Explore Other Integrations\n\nExplore other integration options from Moesif:\n\n- [Server integration options documentation](https://www.moesif.com/docs/server-integration//)\n- [Client integration options documentation](https://www.moesif.com/docs/client-integration/)\n\n[ico-built-for]: https://img.shields.io/badge/built%20for-python%20asgi-blue.svg\n[ico-version]: https://img.shields.io/pypi/v/moesifasgi.svg\n[ico-language]: https://img.shields.io/pypi/pyversions/moesifasgi.svg\n[ico-license]: https://img.shields.io/badge/License-Apache%202.0-green.svg\n[ico-source]: https://img.shields.io/github/last-commit/moesif/moesifasgi.svg?style=social\n\n[link-built-for]: https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface\n[link-package]: https://pypi.python.org/pypi/moesifasgi\n[link-language]: https://pypi.python.org/pypi/moesifasgi\n[link-license]: https://raw.githubusercontent.com/Moesif/moesifasgi/master/LICENSE\n[link-source]: https://github.com/Moesif/moesifasgi","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifasgi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoesif%2Fmoesifasgi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifasgi/lists"}