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:
Now, create a new Python file (e.g., main.py) and type the following code:
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:
Now, open your web browser and go to 'http://localhost:8000/' to see your API in action. You should see the JSON response:
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)