Azure Functions Documentation

Create Your First Azure Function with Visual Studio Code

This guide will walk you through creating your first Azure Function using Visual Studio Code and the Azure Functions extension. We'll create a simple HTTP-triggered function.

Prerequisites

Step 1: Create a New Function Project

1

Open Visual Studio Code. In the Azure view (usually on the left sidebar, click the Azure icon), expand the Functions section.

Azure Functions sidebar in VS Code
2

Click the Create New Project... button (lightning bolt icon). You'll be prompted to select a folder for your project. Choose an empty folder or create a new one.

3

Select a language for your function. For this tutorial, choose JavaScript or TypeScript.

4

Select a template for your first function. Choose HTTP trigger.

5

Provide a name for your HTTP trigger function (e.g., HttpTriggerExample).

6

Choose the authorization level. Select Anonymous for simplicity during development.

VS Code will now create your project structure and install the necessary dependencies.

Step 2: Understand Your Function Code

Open the file corresponding to your function's name (e.g., HttpTriggerExample/index.js or HttpTriggerExample/index.ts). You'll see code similar to this (for JavaScript):

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. 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 function:

Step 3: Run Your Function Locally

1

In the Azure view, click the Start/Attach Debugger button (green play icon). Alternatively, press F5.

2

The Azure Functions Core Tools will start, and your function app will be running locally. You'll see output in the Debug Console, including the URL for your HTTP trigger.

Look for a line like:

Http Functions:
        HttpTriggerExample: [GET,POST] http://localhost:7071/api/HttpTriggerExample
3

Open your web browser and navigate to the URL provided (e.g., http://localhost:7071/api/HttpTriggerExample). You should see the default response message.

4

Now, try passing a name in the query string: http://localhost:7071/api/HttpTriggerExample?name=AzureUser. You should receive a personalized greeting!

Step 4: Deploy to Azure (Optional)

Once you're happy with your function, you can deploy it to Azure.

  1. Sign in to your Azure account using the Azure Functions extension.
  2. In the Azure view, click the Deploy to Function App... button.
  3. Select your subscription, choose an existing Function App or create a new one, and follow the prompts.

Congratulations! You've successfully created and run your first Azure Function locally. This is just the beginning of what you can do with Azure Functions.

Next Steps