Skip to main content
To use AgentPay’s consume() or validate_api_key() methods, your MCP server first needs to extract the User API Key sent by the client. By convention, AgentPay User API Keys are sent in the HTTP header.

Standard Header

The standard HTTP header for transmitting the AgentPay User API Key is: X-AGENTPAY-API-KEY Clients (like Cursor or Claude) should be configured to send the User API Key in this header when using remote MCP Servers.

Extraction in Python Web Frameworks

How you access this header depends on the web framework you are using to run your MCP server. We recommend using Starlette as your web framework, as it is simple, lightweight, and compatible with the official FastMCP framework. Example (Starlette with FastMCP):

Other Frameworks (e.g. Flask, Django, etc.)

If you’re using other Python web frameworks like Flask or Django (or even a standard library http.server), you’ll need to adapt the above Starlette + FastMCP pattern to work with your framework. Here are conceptual examples showing how to integrate User API Key extraction and validation with your framework’s routing: Example (Conceptual - Flask with FastMCP):
Example (Conceptual - Django with FastMCP):
Consult your specific web framework’s documentation on how specifically to adapt these examples to your particular Server’s needs.

Best Practices

  • Case-Insensitivity: Remember that HTTP header names are generally case-insensitive. While X-AGENTPAY-API-KEY is the standard casing, your extraction logic should ideally handle variations if your framework doesn’t normalize them (most modern ones do).
  • Error Handling: Always check if the API key is present. If not, return an appropriate error response to the client (e.g. an appropriate error string, or HTTP 401 Unauthorized).
  • Security: Do not log the full API keys unless absolutely necessary for debugging and ensure logs are secure. Remember, the User API Key you receive will be specific to usage with your Server.

Next Steps

With the User API Key extracted, you can now effectively use: