Context Modules¶
On this page
Overview¶
MongoDB Stitch functions can interact with connected services, user
information, predefined values, and other functions through modules
attached to the global context
variable.
The context
variable contains the following modules:
Property | Description |
---|---|
context.services |
Access service clients for the services you’ve configured. |
context.values |
Access values that you’ve defined. |
context.user |
Access information about the user that initiated the request. |
context.request |
Access information about the HTTP request that triggered this function call. |
context.functions |
Execute other functions in your Stitch application. |
context.http |
Access the HTTP service for get , post , put , patch ,
delete , and head actions. |
Context Modules¶
context.services¶
An object that provides access to service clients for Services in your application, including the MongoDB Service. A service client exposes all actions associated with its service as methods.
-
context.services.
get
(serviceName)¶ Returns a service client object or
undefined
if no such service exists.Parameter Type Description serviceName
string The unique name of the service assigned when the service was created.
You can find the name of the Service associated with a particular external service or linked cluster in the Stitch UI or in an exported application directory:
Stitch UI Refer to the Service Name column of the table on the Services page or the Stitch Service Name column of the table on the Clusters page. Application Directory Refer to the name
field of the service’s config.json file.MongoDB Service Names
The default name for the MongoDB Service associated with a Stitch application’s initial linked cluster is
mongodb-atlas
.
context.functions¶
An object that provides access to other Functions in your application.
-
context.functions.
execute
(functionName, args...)¶ Calls the specified Function with any provided arguments and returns the result.
Parameter Type Description functionName
string The name of the function. args
…mixed A variadic list of arguments to pass to the function. Each function parameter maps to a separate, comma-separated argument.
context.http¶
A general purpose HTTP client that exposes all HTTP service actions and is not affected by service rules.
-
context.http.
get
()¶ Sends an HTTP GET request to the specified URL. See
http.get()
for detailed reference information, including parameter definitions and return types.
-
context.http.
post
()¶ Sends an HTTP POST request to the specified URL. See
http.post()
for detailed reference information, including parameter definitions and return types.
-
context.http.
put
()¶ Sends an HTTP PUT request to the specified URL. See
http.put()
for detailed reference information, including parameter definitions and return types.
-
context.http.
patch
()¶ Sends an HTTP PATCH request to the specified URL. See
http.patch()
for detailed reference information, including parameter definitions and return types.
-
context.http.
delete
()¶ Sends an HTTP DELETE request to the specified URL. See
http.delete()
for detailed reference information, including parameter definitions and return types.
-
context.http.
head
()¶ Sends an HTTP HEAD request to the specified URL. See
http.head()
for detailed reference information, including parameter definitions and return types.
Context Properties¶
context.user¶
A property that contains the user object of the function’s active user.
System Functions
In a system function, context.user
resolves to the authenticated application user that called a function
even though the function executes as the system user and bypasses all
rules. If the function was not called by an authenticated user, such
as in a webhook or trigger, then context.user
resolves to a
system user object that has no data.
Field | Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
id |
string | A string representation of the ObjectId that uniquely identifies the user. | ||||||||
type |
string | The type of the user. The following types are possible:
|
||||||||
data |
document | A document that contains metadata that describes the
user. This field combines the data for all identities
associated with the user, so the exact field names and values
depend on which authentication providers
the user authenticated with. |
||||||||
custom_data |
document | A document from your application’s custom user data collection that specifies the user’s ID. You can use the customer user data collection to store arbitrary data about your application’s users. Stitch automatically fetches a new copy of the data whenever a user refreshes their access token, such as when they log in. The underlying data is a regular MongoDB document, so you can use standard CRUD operations through the MongoDB Atlas service to define and modify the user’s custom data. Avoid Storing Large Custom User Data Custom user data is limited to |
||||||||
identities |
array | A list of authentication provider identities associated with the user. When a user first logs in with a specific provider, Stitch associates the user with an identity object that contains a unique identifier and additional metadata about the user from the provider. For subsequent logins, Stitch refreshes the existing identity data but does not create a new identity. Identity objects have the following form:
|
Example
The following context.user
document reflects an
Email/Password user that is
associated with a single User API Key.
context.request¶
A property that contains information about the external HTTP request that triggered the function call. This information can help you determine the origin of the call. It may contain the following fields:
Field | Type | Description |
---|---|---|
remoteIPAddress |
string | The IP address of the client that issued the Function request. |
requestHeaders |
object | An object where each field maps to a type of HTTP Header that was included in the request that caused the function to execute. The value of each field is an array of strings where each string maps to a header of the specified type that was included in the request. Example |
webhookUrl |
string | Optional. The webhook URL of the incoming webhook that the Function is assigned to, if applicable. |
httpMethod |
string | Optional. The HTTP method of the request that called the incoming webhook associated with the Function, if applicable. |
rawQueryString |
string | The query string from the URL of the incoming HTTP request that caused the function to execute. All query parameters appear in the same order as they were specified. Stitch Removes The Secret Parameter For security reasons, Stitch automatically removes any query
string key/value pair where the key is |
httpReferrer |
string | Optional. The URL of the page that the Function request was issued from. Derived from the Referer HTTP header. |
httpUserAgent |
string | Optional. Characteristic information that identifies the source of the request, such as the software vendor, operating system, or application type. Derived from the User-Agent HTTP header. |
service |
string | Contains the name of the service called within the function. |
action |
string | The action executed on the service. |
Note
If there is no Referer
header or User-Agent
header present in
the request, the respective field will be omitted.
Example
The following context.request
document reflects a function call
issued from https://myapp.example.com/
by a user browsing with
Chrome 73 on macOS High Sierra: