Navigation

Access Function Context

Realm 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.

You can call any function in your application from a function with the context.functions global variable.

// difference: subtracts b from a using the sum function
exports = function(a, b) {
return context.functions.execute("sum", a, -1 * b);
};

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.

exports = function() {
// Instantiate a service client for the HTTP Service named "myHttpService"
const http = context.services.get("myHttpService");
// Call the HTTP service's `get` action (if the service rules allow it)
return http.get({ url: "https://www.mongodb.com" });
};

You can access global values in a function with the context.values global variable.

exports = function() {
// Get a global value (or `undefined` if no value has the specified name)
const theme = context.values.get("theme");
console.log(theme.colors) // Output: { red: "#ee1111", blue: "#1111ee" }
console.log(theme.colors.red) // Output: "#ee1111"
};

You can access the user object of the logged in user that called a function with the context.user global variable.

exports = function() {
const currentUser = context.user
console.log(currentUser.id)
}

You can access information about the external HTTP call that initiated a function's execution with the context.request global variable.

exports = function() {
const request = context.request;
if (request.httpReferrer == "https://www.example.com/") {
applyPercentageDiscount(10);
}
}

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.

exports = function() {
return context.request.requestHeaders;
// e.g. {
// "Content-Type": ["application/json"],
// "Cookie": [
// "someCookie=someValue",
// "anotherCookie=anotherValue"
// ]
// }
}

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.

exports = function(payload, response) {
const rawQuery = context.request.rawQueryString;
// e.g. "?someParam=foo&anotherParam=42"
const query = payload.query
// e.g. { someParam: "foo", anotherParam: 42 }
}
Note
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.

You can determine whether or not a function is being executed as a regular user or a system user by calling context.runningAsSystem().

const isSystemUser = context.runningAsSystem()
if(isSystemUser) {
// Do some work that bypasses rules
} else {
// Do some work in the context of the user that called the function.
}

If the Function is an incoming webhook, you can access the webhook URL on the webhookUrl property of the context.request object:

exports = function(payload, response) {
// Returns the webhook URL as a string
return context.request.webhookUrl
}

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:

exports = function(payload, response) {
switch(context.request.httpMethod) {
case "GET": { /* Handle GET requests */ }
case "POST": { /* Handle POST requests */ }
case "PUT": { /* Handle PUT requests */ }
case "DELETE": { /* Handle DELETE requests */ }
case "PATCH": { /* Handle PATCH requests */ }
default: {}
}
}
Give Feedback