Common API Reference

This document details the common objects and methods available across Azure Functions, regardless of the programming language or trigger type.

Context Object

The context object provides information about the current function execution and allows you to interact with the Azure Functions runtime.

Properties

Name Type Description
invocationId string The unique identifier for the current function invocation.
bindingData object An object containing metadata related to the trigger and bindings. The exact properties vary depending on the trigger.
log Logger Object An object used for writing logs during function execution.
res object For HTTP triggered functions, this is the response object used to send a response back to the client.
done() function A callback function that must be called to signal that the function execution is complete. It can optionally accept an error object or a result object.

Methods

context.done(err, result)

Signals the completion of the function execution.

  • err (optional): An error object if the function execution failed.
  • result (optional): The result of the function execution.

Important Note

For asynchronous operations, you must ensure context.done() is called only after all asynchronous tasks have completed. Using Promises or async/await is highly recommended to manage this.

Logger Object

The log object (accessed via context.log) provides methods for writing diagnostic information during function execution.

Methods

  • log.verbose(message: string): Logs a verbose message.
  • log.debug(message: string): Logs a debug message.
  • log.info(message: string): Logs an informational message.
  • log.warn(message: string): Logs a warning message.
  • log.error(message: string): Logs an error message.

These methods can take multiple arguments, which will be formatted according to standard string interpolation rules.

context.log.info('Processing request for user ID: {userId}', { userId: 123 });

HTTP Trigger Specifics

When a function is triggered by an HTTP request, the context object gains additional properties related to HTTP.

context.req (Request Object)

Represents the incoming HTTP request.

Name Type Description
method string The HTTP method (e.g., 'GET', 'POST').
url string The request URL.
headers object An object containing request headers.
query object An object containing URL query parameters.
params object An object containing route parameters.
body any The request body, parsed according to the Content-Type header.

context.res (Response Object)

Represents the outgoing HTTP response. You set properties on this object to define the response.

Name Type Description
status number The HTTP status code (e.g., 200, 404). Defaults to 200.
body any The response body.
headers object An object containing response headers.

Example of returning an HTTP response:

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,
        body: responseMessage
    };

    context.done();
};