Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Call an Atlas Function

On this page

  • Before You Begin
  • Call a Function

You can call an Atlas Function from a client application using the Atlas Device SDK for Flutter. Functions are serverless JavaScript functions that let you define and execute server-side logic. These server-side Functions can run in the context of the authenticated user, and thus honor the rules, roles, and permissions that you have assigned to your data in Atlas.

For more information on configuring and writing Atlas Functions, refer to Atlas Functions in the App Services documentation.

  1. In an App Services App, define an Atlas Function.

  2. In your client project, initialize the App client.

  3. Then, authenticate a user in your client project.

To call a Function, call User.functions.call(). Pass the Function name as the first argument and all arguments for the Function in a List as the second argument.

To include objects as arguments to the Function, convert them to JSON first. You can do this using the jsonEncode() function included in the built-in dart:convert library.

The Function returns a dynamic value containing MongoDB Extended JSON (EJSON) deserialized to a native Dart object.

final response = await user.functions.call("addition", [1, 2]);
// convert EJSON response to Dart number
print(response);
final responseAsNum = num.tryParse(response["\$numberDouble"]);
prints(responseAsNum); // prints 3

Example

The above client code call this Atlas Function running in an App Services App.

// Add two numbers
exports = function(num1, num2){
return num1 + num2;
};
← 
 →