How to write Azure function in TypeScript?

How to write Azure function in TypeScript?

Now, you can easily create Typescript Azure Functions. Azure functions now support Typescript. Typescript is nothing but a superset of JavaScript that helps you with static typing, different interfaces, and classes that make the development process much easier.

How to write Azure function in TypeScript?

Let’s discuss how to create a typescript Azure Function using Visual Studio Code. Before starting the actual development, we should know what the Prerequisites are needed to create the Azure Function using typescript.

Prerequisites

Below are the prerequisites needed to start with the development

  • Make sure you have an Azure subscription or Azure Account. If you don’t have an Azure account as of now, Create a free Azure Account now.
  • Next thing is, you must have Visual Studio Code installed on your machine. If you have not yet installed it, you can install the Visual Studio Code now.
  • Don’t forget to install the Azure Function Extensions for Visual Studio Code.

Assuming you have all the prerequisites needed here, let’s start with creating the Azure Function Project.

Create the Azure Function Project

  • Open the Visual Studio code IDE, click on the Azure button from the left side, and then click on the Create New Project button as highlighted below.
azure function typescript
  • Browse a location where you want to save your Azure Function project.
  • Make sure to choose the language as Typescript, as highlighted below.
Create a function in Azure with TypeScript
  • On the next window, select the trigger as per your requirement. Here, for the demo perspective, I am choosing the simple and basic trigger, which is the HTTP trigger option, as highlighted below.
typescript azure functions
  • Provide a unique name for your Azure Function Project in the next window, and then press Enter.
  • In the next window, choose the Authorization level based on your requirement, as highlighted below. The available options are Function, Anonymous, and Admin.
How to create a typescript Azure Function using Visual Studio Code

Now, it will take a few seconds to create the Azure Function; you can see the project was created successfully, and below is the index.ts file code.

import { AzureFunction, Context, HttpRequest } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };

};

export default httpTrigger;

Now, if you press F5 to run the Azure function project, you can see that it ran successfully and provided us with the Azure Function URL as highlighted below.

Creating a function in Azure with TypeScript using Visual Studio Code

Test Typescript Azure Function Locally

Now, the Azure Function is created. To make sure the function is working as expected. We can test it locally. Press F5 to run the function. Now you can see the Azure Function project ran successfully and provided us with the below Azure Function URL.

http://localhost:7071/api/myazuretypescriptfunction

Now Open Your Favorite browser and paste the above URL, you can able to see, we got the expected output.

Azure functions typescript example

We tried executing the URL below with the name value as a query string parameter. You can able to see we got the expected output.

How To Test Typescript Azure Function Locally

Deploy Typescript Azure Function To Azure from Visual Studio Code

Since the typescript Azure Function is working properly, let’s deploy the typescript Azure Function to the Azure Portal. Follow the below steps to deploy the typescript Azure Function.

You can click on the Deploy To function App button as highlighted below.

How to Deploy Node js Azure Function

Or else, for the same option, right-click on the Function name —-> Click on the Deploy to Function App option as highlighted below.

Deploy Typescript Azure Function To Azure from Visual Studio Code

Now select the Azure Function App that you have created in the Azure Portal. You can search with your Azure Function App name and then select that.

Note: Make sure to create an Azure Function App in the Azure Portal. You can refer to the above section to create the Azure Function App in the Azure Portal.

How To Deploy Typescript Azure Function To Azure from Visual Studio Code

Now, it will take a few seconds to deploy the Azure Function to the selected Azure Function App successfully.

You may also check out the below-related articles

Final Thoughts

In this Azure article, we discussed how to write Azure functions in TypeScript. Then we discussed how to test Typescript Azure Function Locally, and finally, we discussed how to deploy Typescript Azure Function to Azure from Visual Studio Code.