# 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
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
Create folder
myProject/routes
.Copy
COURSE-NOTES/2.6-transaction-routes/myFirstPipelineRoute.js
into this folder. This code provides the Restify handler to start transaction myPipeline.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'
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
In some cases you can also use your browser.
A Challenge !
We've covered a lot of ground, so let's test your knowledge.
- Create a step
adder
that reads two numbersa
andb
in the input data, and writes the sum of them to the output. - Place this step in a pipeline.
- Create a route for the pipeline
/myProject/1.0/adder/:a/:b
that calls this pipeline. - Show it working in Postman.
Tip: In your route you will need to convert a and b from strings to numbers.