Access Function Context¶
Overview¶
Stitch functions are aware of the context in which they were called and can behave dynamically based on that context. A function’s context includes application components, such as external services and other functions, as well as execution information, such as the profile of the user that was logged in when the function was called.
You can access all aspects of a function’s context in the function’s
source code with the context
global variable.
Application Logic¶
Call a Function¶
You can call any function in your application
from a function with the context.functions
global variable.
Connect to an External Service¶
You can connect to a configured external service from a function by
instantiating a service client with the context.services
global
variable. Any service actions for which you’ve configured rules are
available as methods on the service client.
Access Global Values¶
You can access global values in a function with the context.values
global variable.
Execution Information¶
Get User Information¶
You can access the user object of the logged in user
that called a function with the context.user
global variable.
Get Request Information¶
You can access information about the external HTTP call that initiated a function’s execution with the context.request global variable.
View a Request’s Headers¶
You can use the context.request global variable to access the HTTP Header that were included in the request that caused the function to execute.
View a Request’s Query Parameters¶
You can use the context.request global variable
to access the raw query string from the URL
of the incoming HTTP request that caused a Function to execute. Incoming
webhooks also expose query parameters in the webhook payload
as an
object where each field maps to a query parameter.
Webhook Query Parameters
The fields in payload.query
are not guaranteed to be in the
same order as they were specified in the request. If you need to
preserve the order of the query parameters, use
context.request.rawQueryString
instead.
Check if the Function is Running as a System User¶
You can determine whether or not a function is being executed as a
regular user or a system user by
calling context.runningAsSystem()
.
Incoming Webhook Metadata¶
Get the Current Incoming Webhook URL¶
If the Function is an incoming webhook, you can
access the webhook URL on the webhookUrl
property of the
context.request object:
Get the Current Incoming Webhook HTTP Method¶
If the Function is an incoming webhook, you can
inspect the HTTP method of the request that
called the webhook by accessing the httpMethod
property of the
context.request object: