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.

Top comments (0)