How to create an Atlas schedule trigger using API

Hi, is there a way to create a schedule trigger that runs a function, using the API ?


Yes, you can create and manage functions and triggers using the Atlas App Services Admin API:


Hi Stennie, thanks for your reply, in fact i’m looking for some mongodb API call, to create an Atlas project
schedule trigger that execute a function, i’m not sure if it is possible.

as well if is possible how to create variables and functions in realm(app services) calling the mongodb Atlas API, thanks


The Atlas App Services API links I provided earlier document managing Triggers & Functions (create, list, update, delete, etc). You need to create a function before referencing it in the function_id parameter when creating a trigger via the API. The config object for a trigger defines trigger configuration parameters including the schedule if applicable.

is possible how to create variables and functions in realm(app services)

Yes, this is possible. App Services API calls manage Atlas Functions written in JavaScript so you can use variables and import packages for more complex functions.

I suggest you start by creating some triggers and functions via the Atlas UI. You can then use the App Services API to list your functions and triggers as a reference for creating the same definitions programmatically.


1 Like

Hi Stennie, yes i saw the links, i.e to create a function:
“can_evaluate”: {},
“name”: “string”,
“private”: true,
“source”: “string”,
“run_as_system”: true

the main questions is, how to run the curl command specifying parameters like, Org Id, Project Id etc and the body of the function?, thnaks in advance.


The App Services API docs include some example of making requests via curl:

The documentation for API methods (for example, Creating a new Function) describes the resource endpoints, path parameters, request body schemas, and response schemas.

In your example of creating a function, the source string is the body of the function:

The stringified source code for the function. The code must be valid ES6.

The documentation links above have more detailed information, but I created a few quick examples using curl via zsh.

These assume that shell variables of GROUP_ID, APP_ID, and ACCESS_TOKEN have been appropriately set:

Create an Atlas Function with curl
curl --request POST "$GROUP_ID/apps/$APP_ID/functions" \
  --header "Authorization: Bearer $ACCESS_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{ "name": "gday", "source": "exports = function(name) { return `Hello, ${name ?? \"stranger\"}!` }"}'

List Atlas Functions with curl
# Note that this uses GET request method per the API docs
curl --request GET "$GROUP_ID/apps/$APP_ID/functions" \
  --header "Authorization: Bearer $ACCESS_TOKEN" \
  --header 'Content-Type: application/json' 

Execute Atlas Function (no args) with curl
$ curl --request POST "$GROUP_ID/apps/$APP_ID/debug/execute_function?run_as_system=true" \
  --header "Authorization: Bearer $ACCESS_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{ "name": "gday" }'  
{"error_logs":null,"logs":null,"result":"Hello, stranger!","stats":{"execution_time":"554.776µs"}}%
Execute Atlas Function (passing an arg) with curl
$ curl --request POST "$GROUP_ID/apps/$APP_ID/debug/execute_function?run_as_system=true" \
  --header "Authorization: Bearer $ACCESS_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{ "name": "gday", "arguments": ["Stennie"] }'

{"error_logs":null,"logs":null,"result":"Hello, Stennie!","stats":{"execution_time":"590.229µs"}}%

However, if you’re working with any significant code in Atlas Functions, I recommend using your favourite programming language (eg Python) or a tool like Postman to work with the App Services API. Since curl runs in a shell environment, there is generally more effort involved to debug API calls and properly escape parameters that should (or should not be) interpolated by the shell.


1 Like

thanks Stennie, regarding about how to create a scheduled trigger for a Atlas project in one cluster that execute a function, may you send me some example calling the curl ?, best regards


There are four examples using curl in my previous post. Click on the lines prefixed with :arrow_forward: to show the snippet.

For example:


Hi Stennie, when create a trigger (i.e a trigger that pause one cluster) in one Atlas project, automatically it creates an “app services” called Triggers, this “new app” contains atlas triggers and shows “no environment”, how to get the right APP_ID ?.

the next command doesn´t show anything( in fact there is one app, the one created automatically)

$ curl --request GET --header ‘Authorization: Bearer $ACCESS_TOKEN’ \$GROUP_ID/apps