by davi

Backend Server

Test this app for free
35
import logging

from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from abilities import apply_sqlite_migrations

from models import Base, engine

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()


@app.get("/", include_in_schema=False)
def root():
    return RedirectResponse(url="/docs")


@app.get("/list")
def list_entrypoint():
    some_list = ["data1", "data2"]
    return some_list
Get full code

Created: | Last Updated:

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

FastAPI Backend Server Template Guide

This template provides a ready-to-use FastAPI backend server with API key authentication and SQLite database support. It's perfect for building secure microservices or APIs that require data persistence.

Getting Started

  • Click "Start with this Template" to begin using this template in the Lazy Builder interface.

Initial Setup

You'll need to set up one environment secret:

  • Set the API_KEY environment secret in the Environment Secrets tab. This should be a secure string that will be used to authenticate API requests.

Test the Application

  • Click the Test button to deploy your backend server
  • Lazy will provide you with two URLs:
  • The API endpoint URL
  • The API documentation URL (Swagger UI)

Using the API

To make requests to your API endpoints, you'll need to include your API key in the request headers. Here's an example request to the list endpoint:

bash curl -H "X-API-Key: your_api_key" https://your-api-url/list

Sample response: json ["data1", "data2"]

The API documentation is available at the /docs URL provided by Lazy after deployment. This interactive documentation allows you to: * Browse all available endpoints * Test API calls directly from the browser * View request and response schemas

Extending the API

To add new endpoints, you can follow the existing pattern in the code. For example, to add a new GET endpoint:

python @app.get("/new-endpoint") def new_endpoint(api_key: str = Depends(get_api_key)): return {"message": "New endpoint response"}

For POST endpoints:

python @app.post("/new-post-endpoint") def new_post_endpoint(data: Data, api_key: str = Depends(get_api_key)): return {"message": f"Received: {data.field}"}

The template includes SQLite database support, and you can add new models in the models.py file and create corresponding database migrations in the migrations folder.



Template Benefits

  1. Rapid API Development & Time-to-Market
  2. FastAPI's automatic API documentation and modern syntax enables quick development cycles
  3. Pre-configured authentication system reduces security setup time
  4. Ready-to-use database migrations framework accelerates deployment

  5. Enterprise-Grade Security Integration

  6. Built-in API key authentication system protects sensitive endpoints
  7. Structured security middleware for consistent access control
  8. Environment variable configuration for secure credential management

  9. Scalable Microservices Architecture

  10. Lightweight and high-performance FastAPI framework for optimal resource usage
  11. Modular structure supports easy service expansion
  12. SQLAlchemy integration enables flexible database scaling

  13. Reduced Development Costs

  14. Automated API documentation reduces documentation overhead
  15. Pre-built components minimize boilerplate code writing
  16. Standardized project structure decreases onboarding time for new developers

  17. Enhanced Maintenance & Reliability

  18. Organized codebase with clear separation of concerns
  19. Built-in logging system for better monitoring and debugging
  20. Structured database migration system prevents deployment issues

Technologies

FastAPI Templates and Webhooks FastAPI Templates and Webhooks
Python App Templates for Scraping, Machine Learning, Data Science and More Python App Templates for Scraping, Machine Learning, Data Science and More
Optimize SQL Workflows with Lazy AI: Automate Queries, Reports, Database Management and More Optimize SQL Workflows with Lazy AI: Automate Queries, Reports, Database Management and More

Similar templates

FastAPI endpoint for Text Classification using OpenAI GPT 4

This API will classify incoming text items into categories using the Open AI's GPT 4 model. If the model is unsure about the category of a text item, it will respond with an empty string. The categories are parameters that the API endpoint accepts. The GPT 4 model will classify the items on its own with a prompt like this: "Classify the following item {item} into one of these categories {categories}". There is no maximum number of categories a text item can belong to in the multiple categories classification. The API will use the llm_prompt ability to ask the LLM to classify the item and respond with the category. The API will take the LLM's response as is and will not handle situations where the model identifies multiple categories for a text item in the single category classification. If the model is unsure about the category of a text item in the multiple categories classification, it will respond with an empty string for that item. The API will use Python's concurrent.futures module to parallelize the classification of text items. The API will handle timeouts and exceptions by leaving the items unclassified. The API will parse the LLM's response for the multiple categories classification and match it to the list of categories provided in the API parameters. The API will convert the LLM's response and the categories to lowercase before matching them. The API will split the LLM's response on both ':' and ',' to remove the "Category" word from the response. The temperature of the GPT model is set to a minimal value to make the output more deterministic. The API will return all matching categories for a text item in the multiple categories classification. The API will strip any leading or trailing whitespace from the categories in the LLM's response before matching them to the list of categories provided in the API parameters. The API will accept lists as answers from the LLM. If the LLM responds with a string that's formatted like a list, the API will parse it and match it to the list of categories provided in the API parameters.

Icon 1 Icon 1
108

We found some blogs you might like...