Navigation

Context Modules

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

Integrated External Services are accessible in functions using context.services.

context.services.get(serviceName)

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 suggested name defaults to mongodb-atlas during Stitch application creation. You can verify the name of your MongoDB Atlas service by clicking on Clusters within your Stitch application and viewing the Stitch Service Name

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.

context.functions

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.

context.values

Provides access to constant values defined in MongoDB Stitch.

context.values.get(valueName)

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.

context.user

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

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

context.request

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": "54.173.82.137",
   "httpReferrer": "https://myapp.example.com/",
   "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"
}

context.http

Provides access to an HTTP service with all HTTP actions enabled from the function context. Use this for general HTTP service calls that should not be validated with Service Rules.

Parameters are the same as those accepted by the HTTP methods in the built-in HTTP Service.

  • http.get

    exports = function() {
    
      const http = context.http;
      return http
        .get({url: "https://www.example.com/users" })
        .then(response => {
        // The response body is encoded as raw BSON.binary. Parse it to JSON.
        const ejson_body = EJSON.parse(response.body.text());;
        return ejson_body;
      })
    };
    
  • http.post

    exports = function() {
      const http = context.http;
      return http.post({
          url: "https://www.example.com/messages",
          body: { msg: "This is in the body of a POST request!" },
          encodeBodyAsJSON: true
        })
        .then(response => {
          // The response body is encoded as raw BSON.Binary. Parse it to JSON.
          const ejson_body = EJSON.parse(response.body.text());
          return ejson_body;
        })
    };
    
  • http.put

    exports = function() {
      const http = context.http;
      return http.put({
        url: "https://www.example.com/messages",
        body: { msg: "This is in the body of a PUT request!" },
        encodeBodyAsJSON: true
      })
      .then(response => {
        // The response body is encoded as raw BSON.Binary. Parse it to JSON.
        const ejson_body = EJSON.parse(response.body.text());
        return ejson_body;
      })
    };
    
  • http.patch

    exports = function() {
      const http = context.http;
      return http
        .patch({ url: "https://www.example.com/diff.txt" })
        .then(response => {
          // The response body is encoded as raw BSON.Binary. Parse it to JSON.
          const ejson_body = EJSON.parse(response.body.text());
          return ejson_body;
        })
    };
    
  • http.delete

    exports = function() {
      const http = context.http;
      return http
        .delete({ url: "https://www.example.com/user/8675309" })
        .then(response => {
          // The response body is encoded as raw BSON.Binary. Parse it to JSON.
          const ejson_body = EJSON.parse(response.body.text());
          return ejson_body;
        })
    };
    
  • http.head

    exports = function() {
      const http = context.http;
      return http
        .head({ url: "https://www.example.com/users" })
        .then(response => {
          // The response body is encoded as raw BSON.Binary. Parse it to JSON.
          const ejson_body = EJSON.parse(response.body.text());
          return ejson_body;
        })
    };
    

Context Methods

context.runningAsSystem()

Returns true if the function is running as a System User, otherwise returns false.

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