Navigation

Call a Service Action

Overview

You can call actions associated with a service from Stitch functions, including incoming webhooks and triggers, or directly from a connected client application. Each service action is available as a method on its respective service client.

Note

You must define a service rule that enables an action before you can call it.

Usage

The examples in this section demonstrate calling the post() action from the HTTP Service. Each service action follows a similar pattern but has distinct parameters. For details on the parameters and usage of a specific action, refer to that action’s reference page.

Call from Another Function

To call a service action from a function:

  1. Instantiate a service client from application context using the context.services global variable.
  2. Call the method associated with the action.
const http = context.services.get("myHttpService");
http.post({
    "url": "https://www.example.com",
    "headers": { "Content-Type": ["application/json"] },
    "body": { "msg": "Hello from a service action!" }
  })
  .then(() => "Successfully sent the post request!")

Call from a Client Application

You can call a service action from client applications that are connected with a Client SDK or over the wire protocol.

To call a service action from a JavaScript Client SDK:

  1. Ensure that you have installed or included the SDK service package for the service you wish to call an action on. You can pull in the service package from a CDN or install it with npm.

  2. Import the service client object and any necessary service request objects.

    Note

    Request objects, such as the HTTPMethod object in this example, are helpers that some services use to construct requests in Client SDKs.

  3. Instantiate a service client with the getServiceClient() method.

  4. Create and call a request

const {
  HttpServiceClient,
  HttpRequest,
  HttpMethod
} = stitch;

const app = Stitch.defaultAppClient;
const http = app.getServiceClient(HttpServiceClient.factory, "http");

const request = new HttpRequest.Builder()
  .withMethod(HttpMethod.POST)
  .withUrl("https://www.example.com")
  .withHeaders({ "Content-Type": ["application/json"] })
  .withBody({ "msg": "Hello from a service action!" })
  .build()

http.execute(request)
  .then(console.log)
  .catch(console.error)

To call a service action from the Android Client SDK:

  1. Ensure that you have installed the SDK service package for the service you wish to call an action on.

  2. Import the service client object and any necessary service request objects.

    Note

    Request objects, such as the HTTPMethod object in this example, are helpers that some services use to construct requests in Client SDKs.

  3. Instantiate a service client with the getServiceClient() method.

  4. Create and call a request

StitchAppClient client = Stitch.getDefaultAppClient()
HttpServiceClient http = client.getServiceClient(HttpServiceClient.factory, "http");

HttpRequest request = new HttpRequest.Builder()
  .withMethod(HttpMethod.POST)
  .withUrl("https://www.example.com")
  .withHeaders(new Document("Content-Type", Arrays.asList("application/json")))
  .withBody(new Document("msg", "Hello from a service action!"))
  .build()

http.execute(request).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull final Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("stitch", "Successfully sent the POST request!");
        } else {
            Log.e("stitch", "Error sending POST request:", task.getException());
        }
    }
  });

To call a service action from the iOS Client SDK:

  1. Ensure that you have installed the SDK service Pod for the service you wish to call an action on.

  2. Import the service client object and any necessary service request objects.

    Note

    Request objects, such as the HTTPMethod object in this example, are helpers that some services use to construct requests in Client SDKs.

  3. Instantiate a service client with the serviceClient(fromFactory:withName:) method.

  4. Create and call a request

let app = Stitch.defaultAppClient!
let http = app.serviceClient(
  fromFactory: httpServiceClientFactory,
  withName: "http"
)

let request = HTTPRequest(
  url: "https://www.example.com/diff.txt",
  method: HTTPMethod.post,
  body: ["msg", "This is in the body of a POST request!"]
)

http.execute(request) { result in
  switch result {
  case .success:
    print("Successfully sent POST request!")
  case .failure(let error):
    print("Error sending POST request: \(error)")
  }
}