Rest API using Azure Functions

To create a REST API using Azure Functions, you will need to create a new Azure Functions app and set up HTTP triggers for the functions that you want to expose as API endpoints.

Here is a general outline of the steps you can follow to create a REST API using Azure Functions:

  1. Sign in to the Azure portal and create a new Azure Functions app.
  2. In the Azure Functions app, create a new function. You can choose the HTTP trigger template to create a function that is triggered by an HTTP request.
  3. In the function’s code, specify the HTTP method (GET, POST, PUT, DELETE, etc.) that the function should respond to, and define the logic for handling the request and generating the response.
  4. Test the function by making HTTP requests to the function’s URL using a tool like Postman.
  5. Repeat steps 2-4 for each API endpoint that you want to create.
  6. To secure the API, you can use Azure AD authentication or a custom authentication solution like API keys.

Here is an example of how you can create a REST API using Azure Functions in Python and implement the GET, POST, DELETE, and PUT methods:

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    if req.method == "GET":
        # Handle GET request
        name = req.params.get('name')
        if name:
            return func.HttpResponse(f"Hello {name}!")
        else:
            return func.HttpResponse(
                 "Please pass a name on the query string",
                 status_code=400
            )
    elif req.method == "POST":
        # Handle POST request
        try:
            req_body = req.get_json()
        except ValueError:
            return func.HttpResponse(
                 "Invalid request body",
                 status_code=400
            )
        else:
            name = req_body.get('name')
            if name:
                return func.HttpResponse(f"Hello {name}!")
            else:
                return func.HttpResponse(
                     "Please pass a name in the request body",
                     status_code=400
                )
    elif req.method == "DELETE":
        # Handle DELETE request
        return func.HttpResponse("Deleted successfully", status_code=200)
    elif req.method == "PUT":
        # Handle PUT request
        return func.HttpResponse("Updated successfully", status_code=200)
    else:
        return func.HttpResponse(
             "Invalid request method",
             status_code=405
        )

This function is an HTTP trigger that responds to GET, POST, DELETE, and PUT requests. The GET and POST methods expect a name parameter in the query string or request body, and return a greeting message with the name. The DELETE and PUT methods return a success message. If the name parameter is not provided, the GET and POST methods return an error message. If an invalid request method is used, the function returns an error message.

To test this function, you can use a tool like Postman to send HTTP requests to the function’s URL with the appropriate method. For example, to test the GET request, you can use the following URL:

https://your-function-app.azurewebsites.net/api?name=John

Replace your-function-app.azurewebsites.net with the actual URL of your Azure Functions app

Azure Function Python Developer Guide