For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Tutorial: Call a Third-Party API from a Trigger

In this tutorial, you create a Database Trigger. The Trigger calls a third-party API when a document is inserted into a collection. The Trigger's Function adds the official AWS Software Development Kit (SDK) as an external dependency. The Function then uses the SDK to write the inserted document to an S3 bucket as an Extended JSON object.

The AWS S3 example in this tutorial illustrates a pattern that applies to any third-party service that has a Node.js library:

  • Add the service's npm package as an external dependency.

  • Store any required credentials as Secrets, and reference them from Values in your Function.

  • Import the package with require() inside your Function and call it using the credentials you stored.

To complete this tutorial, you must have:

Create the following Values and Secrets to store your AWS credentials:

  • AwsAccessKeyId: Value that contains your AWS access key ID.

  • AwsSecretAccessKeySecret: Secret that contains your AWS secret access key.

  • AwsSecretAccessKey: Value that links to the Secret. This enables you to access the secret access key in your Function, while still keeping it stored securely as a Secret.

1

In Atlas, go to the Triggers page.

  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Triggers under the Streaming Data heading.

The Triggers page displays.

  1. Click the View All Apps button.

  2. Click the app that you want to create the Trigger in.

  3. In the sidebar, click Values under the Build heading.

2
  1. Click Create a Value.

  2. Enter AwsAccessKeyId as the Value Name.

  3. Select the Value type.

  4. Select the Custom Content option and enter your AWS access key ID.

    Note

    You must enter the access key ID as a string value with quotes ("<accessKeyId>").

  5. Click Save Draft.

3

Note

Secret values cannot be accessed directly, so you must create a second Value that links to the Secret.

  1. Click Create a Value.

  2. Enter AwsSecretAccessKey as the Value Name.

  3. Select the Value type.

  4. Select the Link to Secret option.

  5. Enter AwsSecretAccessKeySecret and click Create "AwsSecretAccessKeySecret" to name the Secret.

    Paste your AWS secret access key into the field that appears below the Secret name.

  6. Click Save Draft to create both the Secret and the Value.

Create a Database Trigger that calls a new Function when a document is inserted. You add the aws-sdk dependency and define the Function code in the same form.

1

In Atlas, go to the Triggers page.

  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Triggers under the Streaming Data heading.

The Triggers page displays.

2
  1. Click Add Trigger to open the Trigger configuration page.

  2. Select the Database Trigger type, then configure the Trigger using the following values:

    Setting
    Value
    Trigger Name

    WriteDocumentToS3Trigger

    Watch Against

    Collection

    Cluster Name

    Name of your test cluster

    Database Name

    Name of your test database

    Collection Name

    Name of your test collection

    Operation Type

    Select only Insert.

    Event Type

    Function

  3. In the Function section, select + New Function.

3
  1. In the Function section, click Add Dependency.

  2. In the Add Dependency window, enter aws-sdk as the Define a Package Name and leave Package Version blank to use the latest version.

  3. Click Add to add the package.

4

Replace YOUR_AWS_REGION and YOUR_S3_BUCKET_NAME with your own AWS Region and the full bucket name from the AWS Management Console.

1/*
2 * Write a newly-inserted document to an S3 bucket as an Extended JSON object.
3 */
4exports = async function writeDocumentToS3(changeEvent) {
5 const { fullDocument } = changeEvent;
6
7 // require calls must be in the exports function
8 const S3 = require("aws-sdk/clients/s3");
9 const s3 = new S3({
10 accessKeyId: context.values.get("AwsAccessKeyId"),
11 secretAccessKey: context.values.get("AwsSecretAccessKey"),
12 region: "YOUR_AWS_REGION",
13 });
14
15 const key = `inserted-documents/${fullDocument._id.toString()}.json`;
16
17 // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
18 await s3.putObject({
19 Bucket: "YOUR_S3_BUCKET_NAME",
20 Key: key,
21 Body: EJSON.stringify(fullDocument),
22 ContentType: "application/json",
23 }).promise();
24
25 return `Wrote document ${fullDocument._id} to ${key}`;
26};

Each time a document is inserted, the Function does the following:

  • Reads the inserted document from the change event's fullDocument field.

  • Creates an S3 client that authenticates with the AWS credentials stored in your Values.

  • Writes the document to the inserted-documents/ prefix of your bucket as <_id>.json.

Note

The Function uses the document's _id as part of the S3 object key. If your documents use an _id type other than ObjectId, the key might not be what you expect.

5

Important

If deployment drafts are enabled for your app, click Review & Deploy to deploy the Values, Secrets, dependency, Function, and Trigger. Until you deploy them, the Trigger does not run and the next step fails.

Insert a test document into the collection that the Trigger watches. Confirm that a corresponding Extended JSON object appears in your S3 bucket under the inserted-documents/ prefix.

1

To learn how, see Connect to a Cluster via mongosh.

2

The following example inserts one document into the test collection. Replace YOUR_DATABASE_NAME and YOUR_COLLECTION_NAME with the database and collection that you entered when you created the Trigger.

// Replace YOUR_DATABASE_NAME and YOUR_COLLECTION_NAME with the
// database and collection that the Trigger watches.
use YOUR_DATABASE_NAME
db.YOUR_COLLECTION_NAME.insertOne({
tutorialTest: "atlas-trigger-s3",
createdAt: new Date()
})

Note the insertedId value from your output. The Function uses this value as the name of the S3 object.

3

In the AWS Management Console, open your S3 bucket and navigate to the inserted-documents/ prefix. An object named <insertedId>.json appears, where <insertedId> is the insertedId value from the previous step.

Note

If the object doesn't appear, review the Trigger's execution logs. To learn how, see Trigger Logs.