# 6 - Transaction Routes

Up till now we've been starting transactions via MONDAT, but that's just a testing tool. Our real objective is to create an API that applications can call to start transactions.

The first, and easiest way to allow clients to start transactions is via the URL shown when your server starts.

curl -X PUT --data '{ \
    "metadata": { "webhook": "http://localhost:33340/notify" }, \
    "data": { "afield": "xyz" } \
}' http://localhost:33370/datp/1.0/tx/start/null
1
2
3
4

While that works it is generally better to define your own route to start the transaction. Some of the benefits are that your route...

  • can provide a simpler interface.
  • can hard code short or long polling (whatever is best for that transaction).
  • validate inputs before starting the transaction.
  • conceal details of your pipelines from the outside world.

# Create a route / handler to start the transaction

  1. Create folder myProject/routes.

  2. Copy COURSE-NOTES/2.6-transaction-routes/myFirstPipelineRoute.js into this folder. This code provides the Restify handler to start transaction myPipeline.

  3. Open COURSE-FILES/2.6-transaction-routes/app.js using Open to the Side, and update your app.js with the code to register the handler above.

GET http://localhost:33370/myProject/1.0/myPipeline
    => handler myPipelineRouteV1()
    => start transaction 'myPipeine'
1
2
3

# Testing the Route

To test this route, call http://localhost:33370/myProject/1.0/myPipeline (opens new window) with Postman.

Or with curl:

curl http://localhost:33370/myProject/1.0/myPipeline
1

In some cases you can also use your browser.


A Challenge !

We've covered a lot of ground, so let's test your knowledge.

  1. Create a step adder that reads two numbers a and b in the input data, and writes the sum of them to the output.
  2. Place this step in a pipeline.
  3. Create a route for the pipeline /myProject/1.0/adder/:a/:b that calls this pipeline.
  4. Show it working in Postman.

Tip: In your route you will need to convert a and b from strings to numbers.

Deployed on Github Pages.
Last updated: 2023-05-17, 15:37:23 UTC