Node.js function invocation context
Invocation context is an object that is accepted by the request handler of a function in Node.js as the second argument.
The invocation context provides additional information about a Node.js function version properties:
functionFolderId: Folder containing the function.functionName: Function ID.functionVersion: Function version ID.memoryLimitInMB: Amount of memory specified when creating the version, MB.requestId: Request ID.token: Parameters for authentication in the APIs of Yandex Cloud services.
The context also contains the following helper methods:
getRemainingTimeInMillis(): Returns the remaining time for the current request, in ms.getPayload(): Returns the request body if HTTP integration is used. By default, HTTP integration is used for all function invocations if?integration=rawis not specified.
Using the invocation context
You can use the invocation context to authenticate with Yandex Cloud APIs and manage how the HTTP request body is retrieved.
Authentication in the Yandex Cloud API
If you specified a service account for a function, the token parameter contains authentication information for that service account:
access_token: IAM token.expires_in: Token lifetime in seconds.token_type: Token type, e.g.,Bearer.
You can also get this information using the metadata service. For more information about this method, see Authentication from within a VM.
Getting the request body
getPayload() is a helper method that simplifies getting the request body. Depending on the content of the request body and header, the function uses different methods to process data:
- If the request body is a JSON document and the header is
Content-Type: application/json, the function returns the result ofJSON.parseexecuted over the request body data. - If the request body contains form parameters and the header is
Content-Type: application/x-www-form-urlencoded, the function returns the result ofquerystring.parse, which is an object with field keys and values.