### Clone and Install Project Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Commands to clone the repository and set up the Python virtual environment. ```bash git clone https://github.com/AsterPay/fastapi-api-monetization.git cd fastapi-api-monetization python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate pip install -r requirements.txt ``` -------------------------------- ### Run Development Server Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Command to start the FastAPI application using Uvicorn. ```bash uvicorn main:app --reload ``` -------------------------------- ### GET / Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Returns API information and pricing details. ```APIDOC ## GET / ### Description Returns API information and pricing. ### Method GET ### Endpoint / ``` -------------------------------- ### Client-Side API Usage Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Example of how a user makes a request to the API using the Python requests library. ```python response = requests.post( "https://your-api.com/generate", headers={"X-API-Key": "user_key"}, json={"prompt": "Hello world"} ) ``` -------------------------------- ### API Endpoint Requests Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Example cURL commands for interacting with the API endpoints. ```bash curl -H "X-API-Key: your_api_key" http://localhost:8000/credits ``` ```bash curl -X POST http://localhost:8000/generate \ -H "X-API-Key: your_api_key" \ -H "Content-Type: application/json" \ -d '{"prompt": "Write a haiku about coding"}' ``` ```bash curl -X POST http://localhost:8000/buy-credits \ -H "X-API-Key: your_api_key" \ -H "Content-Type: application/json" \ -d '{"amount": 100}' ``` -------------------------------- ### GET /credits Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Checks the current credit balance for the authenticated user. ```APIDOC ## GET /credits ### Description Check the current credit balance of the authenticated user. ### Method GET ### Endpoint /credits ### Parameters #### Headers - **X-API-Key** (string) - Required - The user's API key. ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Commands to initialize the environment file and the required configuration structure. ```bash cp .env.example .env ``` ```env # AsterPay ASTERPAY_API_KEY=sk_live_your_api_key ASTERPAY_WEBHOOK_SECRET=whsec_your_webhook_secret # OpenAI (optional) OPENAI_API_KEY=sk-your-openai-key # App APP_URL=http://localhost:8000 ``` -------------------------------- ### Deploy to Fly.io Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Commands to deploy the application to Fly.io. ```bash fly launch fly secrets set ASTERPAY_API_KEY=sk_live_... fly deploy ``` -------------------------------- ### Configure Pricing Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Dictionary structure for defining credit pricing in main.py. ```python pricing = { 50: "3.00", # 50 credits = $3 200: "10.00", # 200 credits = $10 500: "20.00", # 500 credits = $20 } ``` -------------------------------- ### POST /buy-credits Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Initiates a credit purchase and returns a checkout URL for crypto payment. ```APIDOC ## POST /buy-credits ### Description Purchase additional credits. Returns a checkout URL for crypto payment. ### Method POST ### Endpoint /buy-credits ### Parameters #### Headers - **X-API-Key** (string) - Required - The user's API key. #### Request Body - **amount** (integer) - Required - The number of credits to purchase. ``` -------------------------------- ### Deploy to Railway Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Commands to deploy the application to Railway. ```bash railway login railway init railway up ``` -------------------------------- ### Project Directory Structure Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Visual representation of the project file layout. ```text fastapi-api-monetization/ ├── main.py # FastAPI app & endpoints ├── auth.py # API key auth & credit management ├── payments.py # AsterPay integration ├── ai_service.py # AI text generation ├── requirements.txt # Python dependencies ├── .env.example # Environment template └── README.md ``` -------------------------------- ### Custom Endpoint Implementation Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Template for adding new endpoints with credit verification logic. ```python @app.post("/your-feature") async def your_feature( request: YourRequest, api_key: str = Depends(verify_api_key) ): # Check credits credits = get_user_credits(api_key) if credits < 2: # This feature costs 2 credits raise HTTPException(status_code=402, detail="Insufficient credits") # Your logic here result = do_something(request) # Deduct credits deduct_credits(api_key, 2) return {"result": result} ``` -------------------------------- ### POST /generate Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Generates text using an AI service, costing 1 credit per request. ```APIDOC ## POST /generate ### Description Generate text based on a prompt. This operation costs 1 credit. ### Method POST ### Endpoint /generate ### Parameters #### Headers - **X-API-Key** (string) - Required - The user's API key. #### Request Body - **prompt** (string) - Required - The text prompt for the AI. ``` -------------------------------- ### Insufficient Credits Error Response Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md JSON structure returned when a user has insufficient credits. ```json { "error": "insufficient_credits", "message": "Not enough credits", "buy_credits_url": "/buy-credits" } ``` -------------------------------- ### POST /webhook/asterpay Source: https://github.com/asterpay/fastapi-api-monetization/blob/main/README.md Endpoint for receiving payment confirmations from AsterPay. ```APIDOC ## POST /webhook/asterpay ### Description Automatically receives payment confirmations from AsterPay to update user credits. ### Method POST ### Endpoint /webhook/asterpay ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.