Create Your First Azure Function

This guide will walk you through the process of creating your very first Azure Function using Visual Studio Code. We'll create a simple HTTP-triggered function that returns a greeting.

Prerequisites

Steps to Create Your Function

1

Install the Azure Functions Extension for VS Code

Open Visual Studio Code. Navigate to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X) and search for "Azure Functions". Install the extension published by Microsoft.

Azure Functions Extension in VS Code
2

Create a New Project

Once the extension is installed, open the Azure view (click on the Azure icon in the Activity Bar). Under "Functions", click the "Create New Project..." button.

  1. Select a folder location for your project.
  2. Choose a language for your function app. For this tutorial, select JavaScript.
  3. Select a template for your first function. Choose HTTP trigger.
  4. Provide a name for your function (e.g., HttpGreeting).
  5. Choose an authorization level. Select Anonymous for simplicity in this tutorial.

VS Code will now create a new folder with your function files, including index.js (your function code), function.json (configuration), and package.json.

3

Understand the Function Code

Open the index.js file. You'll see code similar to this:

module.exports = async function (context, req) {
    context.log('JavaScript 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
    };
};

This code:

  • Logs a message to the console.
  • Retrieves a name parameter from either the query string or the request body.
  • Constructs a personalized greeting message.
  • Sets the response object with a 200 status code and the greeting message.
4

Run Your Function Locally

Press F5 or go to Run > Start Debugging in VS Code. The Azure Functions Core Tools will start, and your function will be running locally.

You will see output in the terminal, including the local URL for your HTTP trigger. It typically looks like this:

HttpGreeting: [GET,POST] http://localhost:7071/api/HttpGreeting
5

Test Your Function

Open a web browser and navigate to the URL provided in the terminal.

  • To see the default message, just visit: http://localhost:7071/api/HttpGreeting
  • To get a personalized greeting, add a name query parameter: http://localhost:7071/api/HttpGreeting?name=AzureUser

You should see the greeting message displayed in your browser.

Tip: You can also test POST requests with a JSON body using tools like Postman or curl.

Next Steps

Congratulations! You've successfully created and run your first Azure Function. Here are some things you can explore next: