Azure Functions First Function

The function responds to an HTTP request and returns a "Hello, World!" message. This is the simplest possible function and the best starting point to understand how everything fits together.

What This Function Will Do

┌──────────────────────────────────────────────────────────────┐
│                    HELLO WORLD FLOW                          │
│                                                              │
│  Browser / Postman                                           │
│       │                                                      │
│       │  GET http://localhost:7071/api/HelloWorld?name=Ravi  │
│       ▼                                                      │
│  [Azure Function Triggered]                                  │
│       │                                                      │
│       │  Reads "name" from query string                      │
│       │  Builds message: "Hello, Ravi!"                      │
│       ▼                                                      │
│  HTTP Response  ──►  "Hello, Ravi!"                         │
└──────────────────────────────────────────────────────────────┘

Step 1 – Create a New Function App Project

Open a terminal, navigate to a folder where the project should live, and run:

func init HelloWorldApp --worker-runtime javascript
cd HelloWorldApp

This creates a new folder called HelloWorldApp with the standard project files.

What Each File Does

FilePurpose
host.jsonGlobal settings for all functions in this app
local.settings.jsonLocal environment variables like connection strings
.gitignorePrevents sensitive files from being uploaded to Git

Step 2 – Create the Function

Inside the project folder, run:

func new --name HelloWorld --template "HTTP trigger" --authlevel "anonymous"

This command creates a new subfolder called HelloWorld with two files:

HelloWorldApp/
└── HelloWorld/
    ├── function.json   ← Tells Azure: "This is an HTTP trigger"
    └── index.js        ← The code that runs when triggered

Step 3 – Understanding function.json

The function.json file describes the trigger and bindings for the function. For an HTTP trigger it looks like this:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Reading the Configuration

PropertyMeaning
type: httpTriggerThis function starts when an HTTP request arrives
direction: inThis binding brings data INTO the function
authLevel: anonymousNo API key needed to call this function
methods: get, postAccepts both GET and POST HTTP methods
direction: out (second binding)This binding sends data OUT as HTTP response

Step 4 – Writing the Function Code

Open HelloWorld/index.js and replace its contents with:

module.exports = async function (context, req) {

    // Read the "name" value from the query string
    const name = req.query.name || "World";

    // Build the response message
    const message = `Hello, ${name}!`;

    // Send the response back to the caller
    context.res = {
        status: 200,
        body: message
    };
};

Code Walkthrough

LineWhat It Does
contextAzure provides this object — use it to send responses and write logs
reqThe incoming HTTP request — contains query string, body, headers
req.query.nameReads the "name" value from the URL query string
|| "World"If name is missing, default to "World"
context.resThe HTTP response that gets sent back to the browser

Step 5 – Run the Function Locally

Start Azurite first (required even for HTTP functions in newer versions), then in the terminal run:

func start

The terminal displays output similar to:

Azure Functions Core Tools
Functions:

        HelloWorld: [GET,POST] http://localhost:7071/api/HelloWorld

Step 6 – Test the Function

Open a browser and visit:

http://localhost:7071/api/HelloWorld?name=Ravi

The browser shows:

Hello, Ravi!

Visit the URL without the name parameter:

http://localhost:7071/api/HelloWorld

The response is:

Hello, World!

Step 7 – Test with POST Request Using Body

Update the function code to also read from the request body:

module.exports = async function (context, req) {

    // Check query string first, then request body
    const name = req.query.name
        || (req.body && req.body.name)
        || "World";

    context.res = {
        status: 200,
        body: `Hello, ${name}!`
    };
};

Send a POST request using a tool like Postman or curl:

curl -X POST http://localhost:7071/api/HelloWorld \
  -H "Content-Type: application/json" \
  -d '{"name": "Priya"}'

Response:

Hello, Priya!

Summary of What Was Built

┌─────────────────────────────────────────────────────────┐
│              HELLO WORLD – WHAT WAS CREATED             │
│                                                         │
│  Project (Function App)                                 │
│  └── HelloWorld (Function)                              │
│       ├── function.json  → HTTP Trigger config          │
│       └── index.js       → Business logic               │
│                                                         │
│  Trigger:   HTTP GET or POST request                    │
│  Input:     ?name= query string or JSON body            │
│  Output:    Plain text HTTP response                    │
└─────────────────────────────────────────────────────────┘

The first function is now running locally. The next step is to understand all the types of triggers that Azure Functions supports — not just HTTP.

Leave a Comment