DEV Community

Manikanta
Manikanta

Posted on

Create REST API with Azure Function App

When we create a function for an HTTP trigger, the function is addressable with a route of the form
http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>
We can customize this route using the optional route property on the HTTP trigger's input binding.
With this concept of route I tried to implement REST style API for contacts.

Function REST verb & route
CreateContact POST api/contact
GetContacts GET api/contact
GetContactById GET api/contact/{id}
UpdateContact PUT api/contact/{id}
DeleteContact DELETE api/contact/{id}

We can use Visual Studio Code with Azure Functions extensions or directly use the Azure portal for my sample API. For brevity I'm skipping the project creation procedure and focused mainly on the Azure Function implementation to get the desired result. Relied on the in-memory collection for simplicity. The complete code is placed here

.

Create a contact, POST api/contact

        [FunctionName("CreateContact")]
        public static async Task<IActionResult> CreateContact(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "contact")] HttpRequest req, ILogger log)
        {
            log.LogInformation("Creating a new contact");
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var input = JsonConvert.DeserializeObject<ContactModel>(requestBody);

            var contact = new Contact() { FirstName = input.FirstName, LastName = input.LastName, Phone = input.Phone };
            contacts.Add(contact);
            return new OkObjectResult(contact);
        }
Enter fullscreen mode Exit fullscreen mode

Get all contacts, GET api/contact

        [FunctionName("GetContacts")]
        public static IActionResult GetContacts(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "contact")] HttpRequest req, ILogger log)
        {
            log.LogInformation("Getting contacts");
            return new OkObjectResult(contacts);
        }
Enter fullscreen mode Exit fullscreen mode

Get contact by id, GET api/contact/{id}
I'm using the {id} parameter to the Route binding Route = "contact/{id}"

        [FunctionName("GetContactById")]
        public static IActionResult GetContactById(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "contact/{id}")] HttpRequest req,
            ILogger log, string id)
        {
            var contact = contacts.FirstOrDefault(t => t.Id == id);
            if (contact == null)
            {
                log.LogWarning("contact not found");

                return new NotFoundResult();
            }

            log.LogInformation("Getting contact by Id");

            return new OkObjectResult(contact);
        }
Enter fullscreen mode Exit fullscreen mode

Update contact, PUT api/contact/{id}

        [FunctionName("UpdateContact")]
        public static async Task<IActionResult> UpdateContact(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "contact/{id}")] HttpRequest req,
            ILogger log, string id)
        {
            var contact = contacts.FirstOrDefault(t => t.Id == id);
            if (contact == null)
            {
                log.LogWarning("contact not found");

                return new NotFoundResult();
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var updatedContact = JsonConvert.DeserializeObject<ContactModel>(requestBody);

            if (!string.IsNullOrEmpty(updatedContact.FirstName))
            {
                contact.FirstName = updatedContact.FirstName;
            }
            if (!string.IsNullOrEmpty(updatedContact.LastName))
            {
                contact.LastName = updatedContact.LastName;
            }
            if (!string.IsNullOrEmpty(updatedContact.Phone))
            {
                contact.Phone = updatedContact.Phone;
            }

            return new OkObjectResult(contact);
        }        
Enter fullscreen mode Exit fullscreen mode

Delete a contact, DELETE api/contact/{id}

        [FunctionName("DeleteContact")]
        public static IActionResult DeleteContact(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "contact/{id}")] HttpRequest req,
            ILogger log, string id)
        {
            var contact = contacts.FirstOrDefault(t => t.Id == id);
            if (contact == null)
            {
                return new NotFoundResult();
            }
            contacts.Remove(contact);
            return new OkResult();
        }
Enter fullscreen mode Exit fullscreen mode

After these Functions are published in Azure FunctionApp we can take the url as shown in following screenshot
image

and use postman to test the API.

image

References:

  1. Develop Azure Functions with Visual Studio Code
  2. Customize http endpoint in azure function

Top comments (0)