name | description | page_type | languages | products | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|---|
Azure Functions TypeScript HTTP Trigger using AZD |
This repository contains an Azure Functions HTTP trigger quickstart written in TypeScript and deployed to Azure Functions Flex Consumption using the Azure Developer CLI (AZD). This sample uses managed identity and a virtual network to insure it is secure by default. |
sample |
|
|
functions-quickstart-typescript-azd |
This repository contains a Azure Functions HTTP trigger quickstart written in TypeScript and deployed to Azure using Azure Developer CLI (AZD). This sample uses managed identity and a virtual network to insure it is secure by default.
- Node.js 20
- Azure Functions Core Tools
- Azure Developer CLI (AZD)
- Visual Studio Code
- Needed only when using Visual Studio Code to run and debug locally.
- Also requires the Azure Functions extension
- An HTTP test tool that keeps your data secure. This article uses the
curl
tool. For more information, see HTTP test tools.
Run this git
command to clone this repository to your local machine:
git clone https://github.com/Azure-Samples/functions-quickstart-typescript-azd.git
-
Navigate to the new functions-quickstart-typescript-azd folder.
-
Create a file named
local.settings.json
and add the following:{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "node" } }
-
Open the functions-quickstart-typescript-azd folder in a new terminal and run the following commands:
npm install func start
You should see the URL endpoints for the two HTTP triggered functions in the output.
-
Test the HTTP GET trigger by opening the http://localhost:7071/api/httpGetFunction link in your browser or HTTP test tool.
-
Test the HTTP POST trigger in a new terminal window using your HTTP test tool, such as this curl command:
curl -i -X POST http://localhost:7071/api/httppostbodyfunction -H "Content-Type: text/json" --data-binary "@src/functions/testdata.json"
- Open this folder in a new terminal.
- Open Visual Studio Code by running
code .
in the terminal. - Add required files to the
.vscode
folder by opening the command palette usingCrtl+Shift+P
(orCmd+Shift+P
on Mac) and selecting Azure Functions: Initialize project for use with VS Code. - Press Run/Debug (F5) to run in the debugger (select "Debug anyway" if prompted about local emulater not running).
- Use same approach above to test using an HTTP test extension in Visual Studio Code.
The key code that makes tthese functions work is in ./src/functions/httpGetFunction.cs
and .src/functions/httpPostBodyFunction.cs
. The function is identified as an Azure Function by use of the @azure/functions
library.
This code shows a HTTP GET triggered function.
export async function httpGetFunction(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || await request.text() || 'world';
return { body: `Hello, ${name}!` };
};
app.http('httpGetFunction', {
methods: ['GET'],
authLevel: 'function',
handler: httpGetFunction
});
This code shows a HTTP POST triggered function which expects person
object with name
and age
values in the request.
interface Person {
name: string;
age: number;
}
function isPerson(obj: any): obj is Person {
return typeof obj === 'object' && obj !== null &&
typeof obj.name === 'string' &&
typeof obj.age === 'number';
}
export async function httpPostBodyFunction(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
try {
const data: any = await request.json();
if (!isPerson(data)) {
return {
status: 400,
body: 'Please provide both name and age in the request body.'
};
}
return {
status: 200,
body: `Hello, ${data.name}! You are ${data.age} years old.`
};
} catch (error) {
return {
status: 400,
body: 'Invalid request body. Please provide a valid JSON object with name and age.'
};
}
};
app.http('httpPostBodyFunction', {
methods: ['POST'],
authLevel: 'function',
handler: httpPostBodyFunction
});
To provision the dependent resources and deploy the Function app run the following command:
azd up
You're prompted for an environment name (this is a friendly name for storing azd parameters), your Azure subscription, and the location of an Azure region in which to create the resource group.
When you're done with your deployment and to avoid incurring further costs, use the azd down
command to delete the resource group and all its contained resources that you created using azd up
.