Azure Functions API Reference
This document provides a comprehensive reference for the Azure Functions runtime APIs, allowing you to interact with and manage your functions programmatically.
Core Runtime APIs
context Object
The context object is passed to your function at runtime and provides access to essential information and tools.
Properties
context.log: A logger object for writing logs. Methods includelog.info(),log.warn(),log.error().context.bindingData: An object containing data from input bindings and system properties.context.bindings: An object containing input and output bindings defined for the function.context.req: (For HTTP triggered functions) The incoming HTTP request object.context.res: (For HTTP triggered functions) The outgoing HTTP response object.
Methods
context.done(err, result): Signals the completion of the function execution.erris an error object, andresultis the return value.
req Object (HTTP Trigger)
Represents the incoming HTTP request.
Properties
req.method: The HTTP method (e.g., "GET", "POST").req.url: The request URL.req.headers: An object containing request headers.req.query: An object containing query string parameters.req.body: The request body.req.params: An object containing route parameters.
res Object (HTTP Trigger)
Represents the outgoing HTTP response.
Properties
res.status(statusCode): Sets the HTTP status code.res.send(body): Sends the response body.res.headers(headers): Sets custom response headers.res.json(body): Sends a JSON response.
Runtime Management APIs
azureFunctions.registerFunction(functionId, handler)
Registers a new function with the runtime.
Parameters:
functionId(string): A unique identifier for the function.handler(function): The function to execute.
Example: Registering an HTTP Function
const azureFunctions = require('@azure/functions');
azureFunctions.registerFunction('myHttpTrigger', async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || 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,
body: responseMessage
};
context.done();
});
azureFunctions.invokeFunction(functionId, payload)
Invokes another function within the same function app.
Parameters:
functionId(string): The ID of the function to invoke.payload(object): The data to pass to the invoked function.
Example: Invoking Another Function
// Inside Function A
module.exports = async function (context, myQueueItem) {
context.log('Function A received item:', myQueueItem);
const invocationResult = await azureFunctions.invokeFunction('FunctionB', {
message: 'Data from Function A',
originalItem: myQueueItem
});
context.log('Result from Function B:', invocationResult);
context.done();
};
Binding SDKs
Refer to the specific SDK documentation for each binding (e.g., Cosmos DB, Storage Queues, Event Hubs) for their respective APIs.
Common Binding Operations
- Input Bindings: Data is typically available directly via
context.bindingsorcontext.bindingData. - Output Bindings: Assign data to the appropriate property in
context.bindings.
Example: Writing to a Queue Output Binding
// Assuming a queue output binding named 'outputQueue' is configured
module.exports = async function (context) {
context.log('Queue trigger function processed work item.');
const messageToSend = {
id: 123,
status: 'Processed'
};
context.bindings.outputQueue = JSON.stringify(messageToSend);
context.log('Message sent to output queue.');
context.done();
};
System Functions
Internal functions used by the Azure Functions runtime.
$return: Used for returning values from non-HTTP triggered functions, especially in languages like C#. In Node.js,context.done(null, result)is preferred.
HTTP API Endpoints
While functions are typically invoked via triggers, the runtime exposes certain HTTP endpoints for management and invocation purposes.
POST /admin/functions/{functionName}/invoke
Invokes a specific function. Typically used for testing or manual execution.
Request Body:
{
"input": { ... } // The input payload for the function
}
Response:
{
"output": { ... }, // The output from the function
"logs": [ ... ] // Any logs generated during execution
}
Example: Invoking a Function via HTTP
This is a conceptual example. Actual invocation often uses Azure CLI or SDKs.
curl -X POST \
http://localhost:7071/admin/functions/MyFunctionName/invoke \
-H 'Content-Type: application/json' \
-d '{
"input": {
"message": "Hello from curl!"
}
}'