DEV Community

Santosh Shelar
Santosh Shelar

Posted on

Fast API Basic

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. Here's a simple example of how to create a "Hello, World!" API using FastAPI:

Installation: You can install FastAPI and an ASGI server, such as Unicorn, using pip:

Image description

Now, create a new Python file (e.g., main.py) and type the following code:

Image description

This code creates a new FastAPI application and defines a single route (/) that returns a JSON object with the message "Hello, World!".

To run the application, use the following command:

Image description

Now, open your web browser and go to 'http://localhost:8000/' to see your API in action. You should see the JSON response:

Image description

API Documentation:
FastAPI generates interactive API documentation automatically.
You can access it by navigating to 'http://localhost:8000/docs' in your browser. This documentation is generated based on the type hints in your code.

Building Complex APIs:
FastAPI supports advanced features such as query parameters, request bodies, dependencies, security, and more. Dive deeper into building robust APIs with FastAPI's powerful capabilities.

Thanks for reading.

You can also follow me on X for Guides, Tips & Tricks.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay