Introduction

In this second tutorial on flows™, we will, like in the first, create a contract between a transport company and a producer of fragile goods for the transportation of said fragile goods.

The cargo will again include an accelerometer, which will continuously monitor and store the amount of shock the cargo is exposed to, to be fed into the contract on arrival.

This time, however, the trigger flow will not be triggered with the sensor payload by the sensor itself, but instead poll the accelerometer for its readings when it is being triggered.

The contract will then again deduct an agreed-upon amount for every reading that exceeds an agreed-upon threshold.

1.Contract, Signatory & Clause

Let's start by creating a contract, adding a signatory (other than yourself), and adding the fragile-goods clause to the contract:

  • Contracts -> New Contract
  • Edit Title -> Enter Contract Title
  • Add Signatory
  • Add Clause -> fragile-goods

2. Create Trigger flow

Next, we'll create a trigger flow that, when triggered, will poll the accelerometer for its readings, and transform it, as in the first example, into a clause trigger payload using JSONata.

To simulate the accelerometer, we will poll this test service, which creates random sample meter readings:

https://svc-helper-api.clause.io/fragile-goods/random-readings

We will use the HTTP step to make a GET request to the above url, and then use the following JSONata expression in a JSONata step to transform it into a clause payload:

{
    "$class": "io.clause.demo.fragileGoods.DeliveryUpdate",
    "startTime": $now(),
    "finishTime": $now(),
    "status": "ARRIVED",
    "accelerometerReadings": step[1].output
}

Note that the only difference between this JSONata expression and the one in the first example is that step[0].output has been replaced by step[1].output. We're not referencing the trigger payload anymore, but instead the output of the first step (HTTP).

  • Edit Clause
  • Add additional trigger flow
  • Flow Name
  • Trigger Type -> HTTP
  • Add Step -> HTTP
  • HTTP input attributes
  • Add Step -> JSONata
  • JSONata Expression
  • Save

As before, take note of the trigger url and the authentication token in the flow dialog (viewed by editing the trigger flow)

  • Edit Flow

Trigger the flow by making an HTTP POST request to the trigger url retrieved above, with the token set as Bearer token in the Authorization header. This time, the payload is of no consequence, so we won't set one (and since we're not sending any payload, we don't need to define a Content-Type header):

curl -X POST -H "Authorization: Bearer TRIGGER_FLOW_TOKEN" TRIGGER_FLOW_URL

This should result in a response similar to this (the amount will vary depending on the exact random meter readings generated):

{
  "response": {
    "$class": "io.clause.demo.fragileGoods.PayOut",
    "amount": {
      "$class": "org.accordproject.money.MonetaryAmount",
      "doubleValue": 990,
      "currencyCode": "USD"
    },
    "transactionId": "9bf0b153-99e8-42eb-b41d-f0d30f5817cb",
    "timestamp": "2019-10-21T12:13:22.567Z",
    "test": true,
    "message": "Contract 'untitled' (5dad837799b2a6001b8aa35a) has not been signed by all signatories.\nThis response should only be used for testing your API.\n\nOnce the contract has been signed by all signatories:\n- Obligations emitted by clauses will be processed\n- Clauses will maintain state between requests"
  }
}



This is enough to demonstrate that the flow works. To complete this contract, you can complete all the steps from "Create Action Flow" onwards in the first tutorial.

3. Conclusion

In this tutorial we have referenced the output of the first user-defined step in the trigger flow using step[1].output.

Next, we will create an even more complex flow, and reference multiple previous steps in a similar way.