No-Code Fast Api

Sumanta Mukhopadhyay
3 min readFeb 4, 2023

--

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python-type hints. With FastAPI, you can build APIs quickly and easily, with automatic validation and documentation using the OpenAPI and Swagger UI standards.

One of the great features of FastAPI is the ability to generate APIs without writing any code. This can be done using a Command Line Interface (CLI) application that takes user inputs and generates the required endpoints and functionality. In this article, we will build a no-code FastAPI solution using the Typer CLI. Typer is a library for building CLI applications in Python and is built on top of the Click library.

The solution we will build will allow users to generate endpoints for their API with specific names, methods (such as post, put, patch, delete), and inputs. The inputs for each endpoint can be specified as key-value pairs, where the key represents the name of the input and the value represents the data type of the input.

The first step is to install the necessary libraries. You can install FastAPI and Typer by running the following commands in your terminal/command prompt:

pip install fastapi
pip install typer

Here is the code for the no-code FastAPI solution with a Typer CLI:

import typer
from fastapi import FastAPI

app = FastAPI()

def generate_endpoint(endpoint_name: str, endpoint_method: str, endpoint_inputs: dict):
endpoint_input_str = ', '.join([f"{key}: {value}" for key, value in endpoint_inputs.items()])
endpoint_code = f"@app.{endpoint_method}('/{endpoint_name}')\ndef {endpoint_name}({endpoint_input_str}):\n return {{'message': '{endpoint_method} operation for endpoint {endpoint_name}'}}"
exec(endpoint_code)

def main(endpoint_name: str = typer.Option(None), endpoint_method: str = typer.Option(None), endpoint_inputs: dict = typer.Option(None)):
if endpoint_name and endpoint_method and endpoint_inputs:
if endpoint_method in ['post', 'put', 'patch', 'delete']:
generate_endpoint(endpoint_name, endpoint_method, endpoint_inputs)
typer.echo(f"Endpoint {endpoint_name} with method {endpoint_method} created successfully.")
else:
typer.echo(f"Method {endpoint_method} not supported. Please choose from: post, put, patch, delete.")
else:
typer.echo("Please provide all required arguments to generate an endpoint.")

if __name__ == "__main__":
typer.run(main)

The above code is a command line interface (CLI) application built using the Typer library in Python. To run and use the application, follow these steps:

  1. Install the required packages: Make sure you have FastAPI and Typer installed in your environment. If not, you can install them using the following command: pip install fastapi typer
  2. Save the code: Save the code to a file with a .py extension, for example, generate_endpoint.py
  3. Run the CLI: Open a terminal or command prompt, navigate to the folder where the code is saved, and run the following command: python generate_endpoint.py
  4. Provide the required arguments: To generate an endpoint, you need to provide three arguments: endpoint_name, endpoint_method, and endpoint_inputs. For example, if you want to generate a POST endpoint with the name add_user and the inputs username and email, you can run the following command: python generate_endpoint.py --endpoint_name add_user --endpoint_method post --endpoint_inputs '{"username": "str", "email": "str"}'
  5. Check the generated endpoint: The generated endpoint can be found in the FastAPI application defined in the code. You can run the FastAPI application using the following command: uvicorn generate_endpoint:app --reload
  6. Test the endpoint: You can test the generated endpoint using a tool like Postman by sending a POST request to http://localhost:8000/add_user with a JSON payload containing the inputs username and email.

Note: This code is for demonstration purposes only and is not intended for production use without further modifications and security considerations.

--

--

Sumanta Mukhopadhyay
Sumanta Mukhopadhyay

Responses (1)