Function Context

MongoDB Stitch functions can interact with connected services, user information, predefined values, and other functions through the context variable.

  • Services like Twilio or GitHub can be accessed through
  • Functions can execute other functions using context.functions.
  • Information about the requesting user can be accessed through context.user.
  • Information about the HTTP request that triggered a function call can be accessed through context.request.
  • Global variables can be defined in Stitch and referenced with context.values.

Integrated Services are accessible in functions using

Returns a service handle object or undefined if no such service exists.

Parameter Type Description
serviceName string The name of the service. This is the name you provided for the service when it was created. For the MongoDB Atlas service, the name is always mongodb-atlas, and for the Push Notification service, the name is always gcm.
Returns:An object with methods to perform service actions. See Functions for a reference of available actions on each service. If the service does not exist, undefined is returned.


When creating a MongoDB Stitch app through Atlas, a default MongoDB service is added and named “mongodb-atlas”.

exports = function() {
   var mongodb ="mongodb-atlas");
   return mongodb.db("db").collection("coll").findOne();

See also

MongoDB (Atlas).


Other functions defined in MongoDB Stitch may be called using context.functions.

context.functions.execute(functionName, arg...)

Calls another function defined in MongoDB Stitch.

Parameter Type Description
functionName string The name of the function.
arg mixed A variadic list of arguments passed to the function.
Returns:The returned value of the called function.


Suppose the function sum is defined as follows:

exports = function (a, b) { return a + b; };

This can be called from another function difference as follows:

exports = function(a, b) {
    return context.functions.execute("sum", a, -1 * b);


Provides access to constant values defined in MongoDB Stitch.


Calls another function defined in MongoDB Stitch.

Parameter Type Description
valueName string The name of the defined value.
Returns:The value associated with the name or undefined if there is no value defined with name valueName.


Define a value named incrementValue and set the value to 5.

Create a new function named increment with the following code:

exports = function (arg) { return arg + context.values.get("incrementValue"); };


Provides a view of the currently authenticated user. context.user is a document summarized as follows:

    "id": <string>,
    "type": <string>,
    "data": <document>,
    "identities": <array>


The following is an example context.user document:

    "id": "5a09f135b6fc810f19421c12",
    "type": "server",
    "data": {
        "name": "api-key"
    "identities": [
            "id": "5a09f135b6fc810f19421c13",
            "provider_type": "api-key"


Provides information about the external HTTP request that triggered the function call, which can help you determine the origin of the call. It may contain the following fields:

   "remoteIpAddress": <string>,
   "httpReferrer": <string>,
   "httpUserAgent": <string>

If there is no referrer header or user agent header present in the request, the respective field will be omitted.


The following is an example context.request document:

   "remoteIpAddress": "",
   "httpReferrer": "",
   "httpUserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"