Navigation

Service Actions

Each partner service that integrates with MongoDB Stitch offers a number of actions that can be executed from within Stitch functions, or from one of the client SDKs.

These actions let you build application logic in MongoDB Stitch that uses functionality from partner services. For example, the Twilio service offers a send() action that lets you write function code or client application code that can send a text message from a linked Twilio account.

By default, all service actions for a particular service are disabled. To enable a service action, you must specify a service rule that explicitly enables that action. Service rules also allow you to restrict the scope and capabilities of actions in your application. The documentation for each service covers the actions and rules available for that service.

Executing Service Actions from Functions

When building functions in your MongoDB Stitch application, you have access to a context object that gives you access to various aspects of Stitch functionality.

The services field of this object has a get method that lets you access services in your Stitch application by name. The service object returned will have methods for each of the actions available on that service.

For example, if you have a Twilio service in your Stitch application named "my_twilio_service", you can send a text message from a function with the following code sample:

const myTwilioService = context.services.get("my_twilio_service");
myTwilioService.send({
   "to": "+15558675309",
   "from": "+15551234567",
   "body": "I'm using a MongoDB Stitch service action to send this text message from a function!"
})
  .then(() => "text message was successfully sent!")

Note

This code sample will only work if you have a service rule for my_twilio_service that allows the send action with these arguments. See this documentation page for more information on creating service rules.

Executing Service Actions from SDKs

Each MongoDB Stitch client SDK offers the capability to call service actions directly from client application code.

You can create a service object by calling the StitchAppClient.getServiceClient() method.

For example, if you have a Twilio service in your Stitch application named "my_twilio_service", you can send a text message with the following code sample. This sample assumes you have initialized a StitchAppClient.

See Initialize StitchAppClient for more details on initializing a StitchAppClient in your application.

const serviceClient = Stitch.defaultAppClient.getServiceClient(TwilioServiceClient.factory, "my_twilio_service");
serviceClient.sendMessage(
  "+15558675309",
  "+15551234567",
  "I'm using a MongoDB Stitch service action to send this text message!"
).then(() => {
   console.log("Successfully sent text message!");
}).catch(err => {
   console.log("failed to send text message:", err);
});

Note

This code sample will only work if you have a service rule for my_twilio_service that allows the send action with these arguments. See this documentation page for more information on creating service rules.

You can create a service object by calling the StitchAppClient.getServiceClient() method.

For example, if you have a Twilio service in your Stitch application named "my_twilio_service", you can send a text message with the following code sample. This sample assumes you have initialized a StitchAppClient.

See Initialize StitchAppClient for more details on initializing a StitchAppClient in your application.

Stitch.getDefaultAppClient().getServiceClient(TwilioServiceClient.factory, "my_twilio_service")
  .sendMessage(
    "+15558675309",
    "+15551234567",
    "I'm using a MongoDB Stitch service action to send this text message from an Android app!"
   ).addOnCompleteListener(new OnCompleteListener<Void>() {
       @Override
       public void onComplete(@NonNull final Task<Void> task) {
           if (task.isSuccessful()) {
               Log.d("stitch", "Successfully sent text message!");
           } else {
               Log.e("stitch", "Error sending text message:", task.getException());
           }
       }
   });

Note

This code sample will only work if you have a service rule for my_twilio_service that allows the send action with these arguments. See this documentation page for more information on creating service rules.

You can create a service object by calling the StitchAppClient.serviceClient(fromFactory:withName:) method.

For example, if you have a Twilio service in your Stitch application named "my_twilio_service", you can send a text message with the following code sample. This sample assumes you have initialized a StitchAppClient.

See Initialize StitchAppClient for more details on initializing a StitchAppClient in your application.

let twilioClient = Stitch.defaultAppClient!.serviceClient(fromFactory: twilioServiceClientFactory, withName: "my_twilio_service")
twilioClient.sendMessage(
  to: "+15558675309",
  from: "+15551234567",
  body: "I'm using a MongoDB Stitch service action to send this text message from an iOS app!"
) { result in
  switch result {
  case .success:
    print("Successfully sent text message!")
  case .failure(let error):
    print("Error sending text message: \(error)")
  }
}

Note

This code sample will only work if you have a service rule for my_twilio_service that allows the send action with these arguments. See this documentation page for more information on creating service rules.

←   Services Service Rules  →