Learn RESTAPI Development In Azure

Azure

In this article, we will learn how to create a REST API in Azure.

Prerequisites

An Azure subscription. If you don’t have an Azure subscription, you can create a free account.

here we go!

1. Developing a Function App

  • Go to Azure Services, click on “Create a Resource” and then Search for Function App.
  • Click on the Function App and then click on Create.
  • Let us name the resource group “RG-MyCPAPI”.
  • In the function App name, type “cpswapi”.
  • In the runtime stack select “Node.js”.
  • Choose the Version as “14LTS”.
  • Choose the region that is closer to your location.
  • Click on hosting.
  • Create a new storage account and give a name(In my case: “storageaccountmycpsapi”).
  • Try to give a unique name to the storage account.
  • Now, click on Review + Create and then click on Create.
  • That’s it you have created a Function APP.
    App

Function APP

You will see something like this. You have successfully built your Function APP.

2. Establishing a Serverless function

. Go to Apps files. You will find the app name and some code in host.json.

  • Go to the console and type cat host.json; you will see the same code as seen earlier in the App files.
  • Head over to the App services editor(preview) and click on Go, Here also you will find the same file.
  • Head over to the Functions App and then click on functions.
  • click on create.
  • click on the HTTP trigger.
  • In the template, details give a unique name to the new function(It’s a person in our case).
  • select the Authorization level as anonymous.
  • Click on Create.
    HTTP trigger

Person Function

You’ll observe something similar. Congratulations! You have created your serverless function successfully.

3. Examine the Serverless Function

  • Go to the Serverless function we have created.
  • You can find an index.js file similar to host.js as seen before.
  • Go to console, type cd ___(the name of your serverless function), for example, cd person.
  • Then type dir.
  • You will find two files in the person; one is host.js and another one is index.js.
  • Type cat index.js, and you will find the same code as we have seen earlier in the serverless function.
  • Go to function -> Serverless Function (i.e. person)-> Code+Test.
  • Change the index.json to function.json, you will find everything that we have created so far is wrapped in the bindings.
  • Again switch to index.json and click on Test/Run.
  • In the Body change the name as per your wish.
  • Click on Run.
    Output
  • The HTTP trigger function successfully completed
  • You’ll notice a comparable result. Congratulations! Your serverless method has passed testing.

4. Understanding Bindings

  • Let us explore more about the serverless Function Bindings.
  • Go to Integration in the person function we have created, where you can find the architecture of the Function.
  • Our Function is triggered by an HTTP which passes the data into the direct variable.
  • No additional inputs were defined. You can add the inputs if necessary.
  • Click on the HTTP(req) and uncheck the POST from the selected HTTP method because we don’t need it in our project.
  • Now, again go to Code + Test and change the index.js to function.js.
  • Now you can find that methods consist of only “get” and “post” gets removed.
  • If you want to test the function from our application click on the Get function URL.
  • Copy the URL and paste it into the browser.
  • Now, let us pass a name in the query string.
  • Edit the URL with ?name=” Give some name”(In my case it’s: ?name=CP).
  • Hit enter.
    URL
  • The HTTP trigger function successfully completed
  • You’ll notice a comparable phenomenon. Congratulations, you now understand the fundamentals of bindings.

5. Person and Spaceship Function Creation

  • Go to Person Function-> Code + Test->index.js.
  • Change the index.js with the following code.
    module.exports = async function (context, req) {
    context.log('Person HTTP trigger function processed a request.');
    const data ={
    1: 'Keith',
    2:'surya',
    3: 'Morgen',
    }
    const id = req.query.id;
    const responseMessage = id && data[id]
    ? data[id]
    : "Please provide a valid ID"
    context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage
    };
    }
  • Go to Get function URL and copy the URL and paste it in browser.
    Browser
  • Now with the URL add ?id=1. You can find the name according to the id.
    ID
  • Also, try with the id=2 and id=3.
    HTTP
  • Let us try what happens if we use id=4, it will provide the default(i.e. Please provide a valid ID).
  • Let’s again go to index.js and copy the code.
  • Now move to Function APP(cpswapi) and click on Create.
  • Again click on HTTP Trigger and name the function “spaceship”.
  • Click on Create.
    Spaceship
  • Go to the spaceship function -> Code+Test and paste the following code.
    module.exports = async function (context, req) {
    context.log('Planet HTTP trigger function processed a request.');
    const data ={
    1: 'Arun',
    2:'Patel',
    3: 'Kruskal',
    }
    const id = req.query.id;
    const responseMessage = id && data[id]
    ? data[id]
    : "Please provide a valid ID"
    context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage
    };
    }
  • Click save.
  • Go to integration, Click on the HTTP(req), and uncheck the POST from the selected HTTP method because we don’t need it in our project.
  • Head over to Code + Test and click on the Get function URL, copy the URL in the browser.
    HTTP Method
  • Now with the URL add ?id=1. You can find the name according to the id.
    Add
  • Also, try with the id=2 and id=3.
    Download
  • You have successfully generated another serverless function (spaceship).

6. Recognizing Route Templates

  • If we directly give “/3” instead of “?id=3” it will not work. This is the place where Route Templates play a major role.
    Template
  • Go to spaceship function-> integration -> Trigger.
  • Click on HTTP(req) -> Route template and type spaceship/{id: int}.
  • Click on save.
  • Head over to Code + Test.
  • Now, again refresh the page, which didn’t work.
    Code
  • Switch to index.js in the spaceship function and change the following code.
    module.exports = async function (context, req) {
    context.log('Planet HTTP trigger function processed a request.');
    const data ={
    1: 'Arun',
    2:'Patel',
    3: 'Kruskal',
    }
    //changes made here
    const id = context.bindingData.id;
    const responseMessage = id && data[id]
    ? data[id]
    : "Please provide a valid ID"
    context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage
    };
    }
  • Click on save.
  • Now refresh the same page in the browser again. Now you can observe that the name will appear now where “id=3” , by directly placing “/3”.
    Save
  • Congratulations! You’ve mastered the fundamentals of Route templates.

7. Setting up an API Management Instance

  • Go back to our function(cpswapi) -> API Management.
  • Click on Create New.
  • Leave the defaults as it is.
  • Give the organization name (cp) and give your email ID in the administrator email.
  • Click on Review + Create.
  • Wait, it will take a few minutes for the creation.
  • Your App is linked to the API Management instance: ‘cpswapi-apim’.
  • Click on the link API.
  • Keep the defaults as it is and click on Select.
  • Again click Create in the Create from the function App.
    Function App
  • The function API has now been imported.
  • You can see the GET person and the GET spaceship operations.
  • If you click on GET person, you will find a representation of the frontend, Inbound Processing, Outbound Processing, and Backend.
  • If we click on GET spaceship, we will find the id of the integer type that we had defined earlier.
  • Go to settings, and uncheck the subscription required.
  • Click on save.
  • Copy the Base URL.
  • Now you can see the name that is included in the Person function where “ id=1”.
    Integer type
  • Now, you are proficient at creating API Management Instances.

8. Create an Inbound Processing Routing Rule

  • Now, in the API Management, go to design and click on GET Person.
  • In the Frontend click on the edit icon.
  • In the URL, type /Person{id}.
  • In the template parameters, mention “integer” in the TYPE.
  • Click on save.
    Integer
  • If we refresh the page, we will found the message :{ “statusCode”: 404, “message”: “Resource not found” } .
    Resource
  • This happens because our URL is passed to a function, so we need some inbound processing.
  • Click on GET person and in the Inbound Processing click on Add Policy.
  • There are lot of policies available, in our case, we are choosing to rewrite the URL.
  • In the backend, under the GET, type “/person?id={id}”.
  • Click on save.
  • Now let’s go back to our web page, yes it worked
    Web Pages
  • The Inbound Processing Rule for Routing has now been created.

Conclusion

In this article, we covered how to construct a REST API in Azure.

We first constructed a Functions App, then configured a Serverless Function, tested the Serverless Function, and became familiar with the basics of bindings. After that, we set up the route templates, generated an API Management Instance, created the Person and Spaceship functions, and finally, created a Routing Inbound Processing Rule.

Congratulations!! You completed this project and gained extensive knowledge of Azure Basics.

Join my network on LinkedIn! How did you find the manual? Leave a comment and share it on your social media!

Additionally, I appreciate any feedback on anything that is incorrect or missing. Long live the Azure Learning community!