Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Building Production-Grade APIs with FastAPI: The Parts You Actually Need

The first FastAPI app I shipped to production took about two hours to write and three weeks to harden. That ratio is honest-the framework gets you to a working endpoint fast, then leaves you to figure out the database sessions, the auth middleware, the test fixtures, and the worker count that won't fall over at 3pm on a Tuesday. This is the guide I wish someone had handed me before I learned all…

FastAPI is a powerful framework for building production-grade APIs, with features that make the process faster and easier compared to other frameworks. The key to creating a robust FastAPI application lies in understanding its core components and best practices.

One of the advantages of FastAPI is that the same code used for local development can be used in production without any changes. This is possible because FastAPI leverages Starlette and Pydantic v2, which offer fast performance and an async story that outperforms Flask. The framework's appeal comes from the fact that the code can be shipped as-is to production, without the need for rewriting or additional workarounds.

Writing a production-ready FastAPI application takes time and effort. It usually takes about two hours to write the initial app, but then requires around three weeks to harden it properly. This includes addressing issues like database sessions, authentication middleware, test fixtures, and worker management, among others. FastAPI does not cover these aspects in its tutorials, so it's essential to add a process manager, a reverse proxy, and some middleware to ensure a smooth production deployment.

To get started with FastAPI, you'll need Python 3.11 or 3.12 (3.10 also works but lacks certain niceties), an understanding of async/await syntax, and a local Postgres database (or MySQL) for development and testing. Docker can be used to run Postgres if needed, with a simple command to spin up a container. For hitting the API, you can use tools like httpie or curl, and the Swagger UI at /docs for manual testing. For scripted testing, you'll need a CLI.

FastAPI has been successfully used in various real-world applications, such as SaaS billing APIs, ML inference gateways, webhook receivers, and multi-tenant admin panels. The framework's async DB calls can significantly improve performance, as seen in the case of a billing API where the p99 latency dropped from 400ms to 80ms.

When it comes to cost considerations, FastAPI is open-source and free of licensing fees. The main cost comes from infrastructure, with a single small instance (1 vCPU, 1GB RAM) costing around $10-20/month on platforms like Fly.io or Hetzner. This setup can handle 50-100 requests per second (RPS) for typical CRUD operations. Mid-tier instances (2 vCPU, 4GB RAM) cost between $40-80/month and can handle 500 RPS with proper tuning.

It's crucial to avoid running Postgres on the same box as the app in production and to consider managed Postgres solutions for better scalability.

While there are alternative frameworks like Litestar, Django Ninja, Flask + Smorest, and Node.js/Actix/Fiber, FastAPI stands out for its excellent developer experience, built-in tools for OpenAPI documentation, and the ability to handle complex applications with ease. The dependency injection system is particularly powerful, as it allows developers to handle cross-cutting concerns like database sessions, authentication, logging, and rate limiting in a structured manner.

Once developers understand and apply this pattern, FastAPI transforms from a framework into a well-organized function call graph, enabling scalable applications that can grow with the team.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Cloudflare auto-trailing-slash still answers 307. _redirects can do 301.

I looked after two static sites on Cloudflare Workers Assets. Internal links already used the canonical form with or without a trailing slash, depending on whether the page is a directory or a file.

  • Cloudflare auto-trailing-slash returns 307 status codes
  • redirects feature supports 301 status codes
  • Manual testing shows 307 for typo URLs

My attempt at SEO for rayr.uk

I own rayr.uk and I want it to show up when people search "rayr uk" or "ray r uk". Right now it doesn't really. The site is one page with a neon animation and an email button, which looks fine but…

  • Added title, description, canonical link, and Open Graph tags to HTML
  • Implemented structured data linking "rayr.uk", "Ray R UK", and "RayR"
  • Created accounts on social media platforms with consistent usernames

OpenTelemetry Java agent for Dynatrace: instrument an application and send its data

Your team wants standard, portable instrumentation for a Spring Boot application without changing its code. It has no OneAgent on these containers, or it has chosen OpenTelemetry as the platform…

  • OpenTelemetry Java agent instruments Spring Boot applications without code changes
  • Requires Java 8+, Dynatrace API token with specific scopes
  • OTLP endpoint URL varies by Dynatrace setup (SaaS, Managed, ActiveGate)

More from Thursday 26 November →