Context
On this page
- Get App Metadata (
context.app
) - context.app.id
- context.app.clientAppId
- context.app.name
- context.app.projectId
- context.app.deployment
- context.app.lastDeployed
- context.app.hostingUri
- Call a Function (
context.functions
) - context.functions.execute()
- Check the App Environment (
context.environment
) - context.environment.tag
- context.environment.values
- Connect to a MongoDB Data Source or Third-Party Service (
context.services
) - context.services.get()
- Get Request Metadata (
context.request
) - context.request
- Get User Data (
context.user
) - context.user
- context.runningAsSystem()
- Reference a Value (
context.values
) - context.values.get(valueName)
- Send an HTTP Request (
context.http
) - context.http.get()
- context.http.post()
- context.http.put()
- context.http.patch()
- context.http.delete()
- context.http.head()
Atlas Functions have access to a global context
object that contains
metadata for the incoming requests and provides access to components and
services in your App.
The context
object exposes the following interfaces:
Property | Description |
---|---|
Access metadata about the app running the Function. | |
Access environment values and the current environment tag. | |
A client object that calls your app's Functions. | |
A built-in HTTP client. | |
Describes the incoming request that triggered a Function call. | |
Exposes client objects that can access data sources and services. | |
Describes the user that initiated the request. | |
Contains static global values. |
Get App Metadata (context.app
)
The context.app
object contains metadata about the App that
contains the Function.
{ "id": string, "clientAppId": string, "name": string, "projectId": string, "deployment": { "model": string, "providerRegion": string, }, "lastDeployed": string, "hostingUri": string, }
context.app.id
The unique internal ID of the App that contains the Function.
"60c8e59866b0c33d14ee634a"
context.app.clientAppId
The unique Client App ID for the App that contains the Function.
"myapp-abcde"
context.app.name
The name of the App that contains the Function.
"myapp"
context.app.projectId
The ID of the Atlas Project that contains the App.
"5e1ec444970199272441a214"
context.app.deployment
An object that describes the App's deployment model and region.
{ "model": "LOCAL", "providerRegion": "aws-us-east-1" }
context.app.lastDeployed
The date and time that the App was last deployed, formatted as an ISODate string.
"2022-10-31T12:00:00.000Z"
context.app.hostingUri
If static hosting is enabled, this value is the base URL for hosted assets. (Static Hosting is deprecated. Learn more.)
"myapp-abcde.mongodbstitch.com"
Call a Function (context.functions
)
You can call any Function in your application
through the context.functions
interface.
context.functions.execute()
Calls a specific Function and returns the result.
context.functions.execute(functionName, ...args)
Parameter | Type | Description |
---|---|---|
| string | The name of the Function. |
| mixed | A variadic list of arguments to pass to the Function. Each Function parameter maps to a separate, comma-separated argument. |
// difference: subtracts b from a using the sum function exports = function(a, b) { return context.functions.execute("sum", a, -1 * b); };
Check the App Environment (context.environment
)
You can access information about your App's current environment configuration
and access environment-specific values through the context.environment
interface.
context.environment.tag
The name of the app's current environment as a string.
Possible values:
""
"development"
"testing"
"qa"
"production"
exports = async function() { switch(context.environment.tag) { case "": { return "There is no current environment" } case "development": { return "The current environment is development" } case "testing": { return "The current environment is testing" } case "qa": { return "The current environment is qa" } case "production": { return "The current environment is production" } } };
context.environment.values
An object where each field maps the name of an environment value to its value in the current environment.
exports = async function() { const baseUrl = context.environment.values.baseUrl };
Connect to a MongoDB Data Source or Third-Party Service (context.services
)
You can access a client for a linked MongoDB Atlas cluster or
federated data source through the context.services
interface.
context.services.get()
Gets a service client for the specified service or undefined
if
no such service exists.
context.services.get(serviceName)
Parameter | Type | Description |
---|---|---|
| string | The name of the linked cluster, federated database instance, or service. Linked data sources created by your app use one of the following default names:
|
exports = async function() { // Get the cluster's data source client const mdb = context.services.get("mongodb-atlas"); // Reference a specific database/collection const db = mdb.db("myApp"); const collection = db.collection("myCollection"); // Run a MongoDB query return await collection.find({ name: "Rupert", age: { $lt: 50 }, }) };
Get Request Metadata (context.request
)
You can access information about the incoming request with the context.request interface.
Tip
The context.request
interface does not include request body
payloads.
context.request
An object that contains information about the HTTP request that caused the Function to execute.
{ "remoteIPAddress": <string>, "requestHeaders": <object>, "webhookUrl": <string>, "httpMethod": <string>, "rawQueryString": <string>, "httpReferrer": <string>, "httpUserAgent": <string>, "service": <string>, "action": <string> }
Field | Type | Description |
---|---|---|
| string | The IP address of the client that issued the Function request. |
| 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. |
| string | Optional. In HTTPS endpoint Functions, the route of the endpoint. |
| string | Optional. In HTTPS endpoint Functions, the HTTP method of the request that called the endpoint. |
| string | The query string attached to the incoming HTTP request. All query parameters appear in the same order as they were specified. Important! For security reasons, Atlas automatically removes
any query string key/value pair where the key is |
| string | Optional. The URL of the page from which the request was sent. This value is derived from the HTTP Referer header. If the request did not include a
|
| string | Optional. Characteristic information that identifies the source of the request, such as the software vendor, operating system, or application type. This value is derived from the HTTP User-Agent header. If the request did not include a
|
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:
exports = function() { return context.request }
{ "remoteIPAddress": "54.173.82.137", "httpReferrer": "https://myapp.example.com/", "httpUserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36", "rawQueryString": "?someParam=foo&anotherParam=42", "requestHeaders": { "Content-Type": ["application/json"], "Cookie": [ "someCookie=someValue", "anotherCookie=anotherValue" ] } }
Get User Data (context.user
)
You can access information about the application or system user that
called a Function with the context.user
interface.
context.user
The user object of the authenticated user that called the Function.
{ "id": <string>, "type": <string>, "data": <document>, "identities": <array> }
Field | Type | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| string | A string representation of the ObjectId that uniquely identifies the user. | ||||||||||||||||
| string | The type of the user. The following types are possible:
| ||||||||||||||||
| document | A document that contains metadata that describes the
user. This field combines the data for all In system functions, the | ||||||||||||||||
| document | A document from your application's custom user
data collection that
specifies the user's ID. You can use the custom user data
collection to store arbitrary data about your application's
users. If you set the Custom user data is limited to | ||||||||||||||||
| array | A list of authentication provider identities associated with the user. When a user first logs in with a specific provider, App Services associates the user with an identity object that contains a unique identifier and additional metadata about the user from the provider. For subsequent logins, App Services refreshes the existing identity data but does not create a new identity. Identity objects have the following form:
|
context.runningAsSystem()
Evaluates to a boolean that is true
if the Function is running as
a system user.
exports = function() { const isSystemUser = context.runningAsSystem() if(isSystemUser) { // Do some work with the system user. } else { // Fail. } }
Reference a Value (context.values
)
You can access your app's static values in a
Function with the context.values
interface.
context.values.get(valueName)
Gets the data associated with the provided Value name or
undefined
if no such value exists. This data is either a plain
text JSON value or a Secret exposed through a
value.
Parameter | Type | Description |
---|---|---|
| string | The name of the Value. |
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" };
Send an HTTP Request (context.http
)
You can send HTTPS requests through a built-in client with the
context.http
interface.
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.
exports = async function() { const response = await context.http.get({ url: "https://www.example.com/users" }) // The response body is a BSON.Binary object. Parse it and return. return EJSON.parse(response.body.text()); };
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.
exports = async function() { const response = await context.http.post({ url: "https://www.example.com/messages", body: { msg: "This is in the body of a POST request!" }, encodeBodyAsJSON: true }) // The response body is a BSON.Binary object. Parse it and return. return EJSON.parse(response.body.text()); };
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.
exports = async function() { const response = await context.http.put({ url: "https://www.example.com/messages", body: { msg: "This is in the body of a PUT request!" }, encodeBodyAsJSON: true }) // The response body is a BSON.Binary object. Parse it and return. return EJSON.parse(response.body.text()); };
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.
exports = async function() { const response = await context.http.patch({ url: "https://www.example.com/diff.txt", body: { msg: "This is in the body of a PATCH request!" }, encodeBodyAsJSON: true }) // The response body is a BSON.Binary object. Parse it and return. return EJSON.parse(response.body.text()); };
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.
exports = async function() { const response = await context.http.delete({ url: "https://www.example.com/user/8675309" }) };
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.
exports = async function() { const response = await context.http.head({ url: "https://www.example.com/users" }) // The response body is a BSON.Binary object. Parse it and return. EJSON.parse(response.body.text()); };