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:
functionName
: The function ID.functionVersion
: The ID of the function version.memoryLimitInMB
: The amount of memory specified when creating the version, MB.requestId
: The ID of the request being handled.token
: Yandex Cloud API authentication parameters.
The context also contains the following helper methods:
getRemainingTimeInMillis()
: Returns the time, in ms, remaining to complete the current request.getPayload()
: Returns the request body if HTTP integration. By default, HTTP integration is used for all functions invoked if no?integration=raw
parameter is specified.
Using the invocation context
You can use the invocation context to authenticate in the Yandex Cloud API and manage the process of getting the HTTP request body (the body
parameter).
Authentication in the Yandex Cloud API
If a function has a service account specified, the token
parameter contains information for authentication on behalf of this service account:
access_token
: IAM token.expires_in
: Token lifetime in seconds.token_type
: Token type (Bearer
).
You can also get this information using the metadata service. For more information, see Authenticating from inside a VM.
Getting the request body
getPayload()
is a helper method that makes it easier to get the request body (the body
parameter). Depending on the content of the request body and header, the function handles data using different methods:
- If a JSON document is passed as the request body and the header is set to
Content-Type: application/json
, the function returns the result of executing theJSON.parse
method over the data that is passed in the request body. - If form parameters are passed as the request body and the header is set to
Content-Type: application/x-www-form-urlencoded
, the function returns the result of executing thequerystring.parse
method: an object containing field keys and passed values.