ClearRoad is an online platform that aims to centralize data in the Road Usage Charging industry and standardize its format.
Many actors in the Road Usage Charging industry can simplify their IT integration needs by using the ClearRoad platform through its API.
This document aims to provide complete steps on how to quickly get set-up to start building great applications using the ClearRoad API.
All API interactions with ClearRoad are described in this document. The ClearRoad API is focused around the synchronization of data rather than a specification of API messages. To learn more about the ClearRoad API messages, please review the API documentation.
QuickStart
Step 1. Install the library as dependency
We are using npm to install the ClearRoad api library.
Open a terminal window in your project and install the dependency using the command:
npm i @clearroad/api -S
If you want to include the files directly in your page, you can include via our CDN:
To create an instance, you will need to have the apiUrl (the url of the ClearRoad API instance) and an accessToken.
If you do not have an access token, you can request one on the developer platform.
// if using .envCLEARROAD_URL=<url>CLEARROAD_ACCESS_TOKEN=<accesstoken>DROPBOX_ACCESS_TOKEN=<accesstoken>constdotenv=require('dotenv');dotenv.config();constcr=newClearRoad(process.env.CLEARROAD_URL,process.env.CLEARROAD_ACCESS_TOKEN,{localStorage:{type:'dropbox',accessToken:process.env.DROPBOX_ACCESS_TOKEN}});
The ClearRoad API is based on a synchronization process, which will effectively synchronize data from the ClearRoad Platform to a "local" storage. This local storage can be on a local server, on the browser, or on a remote server.
By default, the API will use indexeddb as local storage. To use another storage please refer to the API reference.
Retrieve the Road Account associated with a Road Account Message:
constreference='my-account-reference';awaitcr.post({portal_type:'Road Account Message',account_reference:reference,...});// use reference in search as "reference"cr.allDocs({query:'portal_type: "Road Account" AND reference: "'+reference+'"',select_list:['source_reference','registrations']});
API Reference
ClearRoad
The ClearRoad class contains a subset of functions from the underlying jIO.js library, which uses RSVP.js to chain functions like Promises.
Please refer to their documentation for more information.
constructor
newClearRoad('apiUrl','accessToken');
constructor(url, accessToken, options)
Initialise a new ClearRoad object to interact with the ERP5 storage.
Param
Type
Description
Required
url
string
ClearRoad API url.
Yes
accessToken
string
Access token to authenticate on the ClearRoad API (if necessary).
cr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})}).then(function(id){// 'id' is the posted document 'source_reference'})
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
post(data)
Returns:string The id of the posted message.
Posts data in your local storage and return the reference of the new document.
Then use the sync method to synchronize the data with the ClearRoad API.
Param
Type
Description
Required
data
postData
The message to post. Each value paired with a key must be a string.
Yes
state
cr.post({...}).then(function(reference){// posting a message returns the reference of the message// use reference to get the state of the messagereturncr.state(reference);}).then(function(state){// state = 'processed'});
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
state(id)
Returns:ValidationStates The [state](#api-reference-clearroad-enums-validationstates) of the message.
Check for the processing state of the message.
Allow some time after synchronizing before checking for the state.
Param
Type
Description
Required
id
string
The id of the message.
Yes
sync
cr.sync();
sync(progress)
Returns:IQueue<void>
Synchronizes the local storage with the ClearRoad Platform (will make sure both storage contain the same data).
Param
Type
Description
Required
progress
syncProgressCallback
Function to get notified of progress. There are 4 storages to sync. (default: () => { })
Retrieve the messages in a certain "processing" state.
By default, when a message is not yet synchronized or processed, the state is not_processed.
Param
Type
Description
Required
state
ValidationStates
State of the message.
Yes
options
Partial<IJioQueryOptions>
Set { sort_on, limit } on the results. (default: {})
No
allDocs
Query the documents from ClearRoad Platform:
cr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false}).then(function(result){// read rows in result.rows})
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
Which returns object in the following format:
// with select_list: ['select_list_key']{"total_rows":39,"rows":[{"id":"text_id","value":{"select_list_key":"select_list_value"}},...]}// with include_docs = true{"total_rows":39,"rows":[{"id":"text_id","doc":{"key":"value"}},...]}
allDocs(options)
Returns:IQueue<IJioQueryResults> Search results.
Query for documents in the local storage. Make sure .sync() is called before.
Messages updated before this date will not be synchronized. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
syncPortalTypes
PortalTypes[]
View PortalTypes. Defines which types of messages to synchronize. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
maxSyncObjects
number
Maximum number of objects that will be sycnrhonized from the ClearRoad platform to the local storage. Default is 1234567890.
Yes
useQueryStorage
boolean
Force using a query storage around the localStorage. Needed if the storage can not query data directly. See information on the storage.
Yes
debug
boolean
Log to console replication steps between local and remote storage.
This content is hidden by default. Click on "Show" above or in the menu to view it.
ClearRoad
The ClearRoad class contains a subset of functions from the underlying jIO.js library, which uses RSVP.js to chain functions like Promises.
Please refer to their documentation for more information.
constructor
newClearRoad('apiUrl','accessToken');
constructor(url, accessToken, options)
Initialise a new ClearRoad object to interact with the ERP5 storage.
Param
Type
Description
Required
url
string
ClearRoad API url.
Yes
accessToken
string
Access token to authenticate on the ClearRoad API (if necessary).
cr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})}).then(function(id){// 'id' is the posted document 'source_reference'})
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
post(data)
Returns:string The id of the posted message.
Posts data in your local storage and return the reference of the new document.
Then use the sync method to synchronize the data with the ClearRoad API.
Param
Type
Description
Required
data
postData
The message to post. Each value paired with a key must be a string.
Yes
state
cr.post({...}).then(function(reference){// posting a message returns the reference of the message// use reference to get the state of the messagereturncr.state(reference);}).then(function(state){// state = 'processed'});
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
state(id)
Returns:ValidationStates The [state](#api-reference-clearroad-enums-validationstates) of the message.
Check for the processing state of the message.
Allow some time after synchronizing before checking for the state.
Param
Type
Description
Required
id
string
The id of the message.
Yes
sync
cr.sync();
sync(progress)
Returns:IQueue<void>
Synchronizes the local storage with the ClearRoad Platform (will make sure both storage contain the same data).
Param
Type
Description
Required
progress
syncProgressCallback
Function to get notified of progress. There are 4 storages to sync. (default: () => { })
Retrieve the messages in a certain "processing" state.
By default, when a message is not yet synchronized or processed, the state is not_processed.
Param
Type
Description
Required
state
ValidationStates
State of the message.
Yes
options
Partial<IJioQueryOptions>
Set { sort_on, limit } on the results. (default: {})
No
allDocs
Query the documents from ClearRoad Platform:
cr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false}).then(function(result){// read rows in result.rows})
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
Which returns object in the following format:
// with select_list: ['select_list_key']{"total_rows":39,"rows":[{"id":"text_id","value":{"select_list_key":"select_list_value"}},...]}// with include_docs = true{"total_rows":39,"rows":[{"id":"text_id","doc":{"key":"value"}},...]}
allDocs(options)
Returns:IQueue<IJioQueryResults> Search results.
Query for documents in the local storage. Make sure .sync() is called before.
Messages updated before this date will not be synchronized. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
syncPortalTypes
PortalTypes[]
View PortalTypes. Defines which types of messages to synchronize. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
maxSyncObjects
number
Maximum number of objects that will be sycnrhonized from the ClearRoad platform to the local storage. Default is 1234567890.
Yes
useQueryStorage
boolean
Force using a query storage around the localStorage. Needed if the storage can not query data directly. See information on the storage.
Yes
debug
boolean
Log to console replication steps between local and remote storage.
This content is hidden by default. Click on "Show" above or in the menu to view it.
ClearRoad
The ClearRoad class contains a subset of functions from the underlying jIO.js library, which uses RSVP.js to chain functions like Promises.
Please refer to their documentation for more information.
constructor
newClearRoad('apiUrl','accessToken');
constructor(url, accessToken, options)
Initialise a new ClearRoad object to interact with the ERP5 storage.
Param
Type
Description
Required
url
string
ClearRoad API url.
Yes
accessToken
string
Access token to authenticate on the ClearRoad API (if necessary).
cr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})}).then(function(id){// 'id' is the posted document 'source_reference'})
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
post(data)
Returns:string The id of the posted message.
Posts data in your local storage and return the reference of the new document.
Then use the sync method to synchronize the data with the ClearRoad API.
Param
Type
Description
Required
data
postData
The message to post. Each value paired with a key must be a string.
Yes
state
cr.post({...}).then(function(reference){// posting a message returns the reference of the message// use reference to get the state of the messagereturncr.state(reference);}).then(function(state){// state = 'processed'});
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
state(id)
Returns:ValidationStates The [state](#api-reference-clearroad-enums-validationstates) of the message.
Check for the processing state of the message.
Allow some time after synchronizing before checking for the state.
Param
Type
Description
Required
id
string
The id of the message.
Yes
sync
cr.sync();
sync(progress)
Returns:IQueue<void>
Synchronizes the local storage with the ClearRoad Platform (will make sure both storage contain the same data).
Param
Type
Description
Required
progress
syncProgressCallback
Function to get notified of progress. There are 4 storages to sync. (default: () => { })
Retrieve the messages in a certain "processing" state.
By default, when a message is not yet synchronized or processed, the state is not_processed.
Param
Type
Description
Required
state
ValidationStates
State of the message.
Yes
options
Partial<IJioQueryOptions>
Set { sort_on, limit } on the results. (default: {})
No
allDocs
Query the documents from ClearRoad Platform:
cr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false}).then(function(result){// read rows in result.rows})
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
Which returns object in the following format:
// with select_list: ['select_list_key']{"total_rows":39,"rows":[{"id":"text_id","value":{"select_list_key":"select_list_value"}},...]}// with include_docs = true{"total_rows":39,"rows":[{"id":"text_id","doc":{"key":"value"}},...]}
allDocs(options)
Returns:IQueue<IJioQueryResults> Search results.
Query for documents in the local storage. Make sure .sync() is called before.
Messages updated before this date will not be synchronized. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
syncPortalTypes
PortalTypes[]
View PortalTypes. Defines which types of messages to synchronize. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
maxSyncObjects
number
Maximum number of objects that will be sycnrhonized from the ClearRoad platform to the local storage. Default is 1234567890.
Yes
useQueryStorage
boolean
Force using a query storage around the localStorage. Needed if the storage can not query data directly. See information on the storage.
Yes
debug
boolean
Log to console replication steps between local and remote storage.
This content is hidden by default. Click on "Show" above or in the menu to view it.
ClearRoad
The ClearRoad class contains a subset of functions from the underlying jIO.js library, which uses RSVP.js to chain functions like Promises.
Please refer to their documentation for more information.
constructor
newClearRoad('apiUrl','accessToken');
constructor(url, accessToken, options)
Initialise a new ClearRoad object to interact with the ERP5 storage.
Param
Type
Description
Required
url
string
ClearRoad API url.
Yes
accessToken
string
Access token to authenticate on the ClearRoad API (if necessary).
cr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})}).then(function(id){// 'id' is the posted document 'source_reference'})
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
// 'id' is the posted document 'source_reference'constid=awaitcr.post({key1:"value",key2:JSON.stringify({"subkey":"subvalue"})});
post(data)
Returns:string The id of the posted message.
Posts data in your local storage and return the reference of the new document.
Then use the sync method to synchronize the data with the ClearRoad API.
Param
Type
Description
Required
data
postData
The message to post. Each value paired with a key must be a string.
Yes
state
cr.post({...}).then(function(reference){// posting a message returns the reference of the message// use reference to get the state of the messagereturncr.state(reference);}).then(function(state){// state = 'processed'});
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
// posting a message returns the reference of the messageconstreference=awaitcr.post({...});// use reference to get the state of the messageconststate=awaitcr.state(reference);// state = 'processed'
state(id)
Returns:ValidationStates The [state](#api-reference-clearroad-enums-validationstates) of the message.
Check for the processing state of the message.
Allow some time after synchronizing before checking for the state.
Param
Type
Description
Required
id
string
The id of the message.
Yes
sync
cr.sync();
sync(progress)
Returns:IQueue<void>
Synchronizes the local storage with the ClearRoad Platform (will make sure both storage contain the same data).
Param
Type
Description
Required
progress
syncProgressCallback
Function to get notified of progress. There are 4 storages to sync. (default: () => { })
Retrieve the messages in a certain "processing" state.
By default, when a message is not yet synchronized or processed, the state is not_processed.
Param
Type
Description
Required
state
ValidationStates
State of the message.
Yes
options
Partial<IJioQueryOptions>
Set { sort_on, limit } on the results. (default: {})
No
allDocs
Query the documents from ClearRoad Platform:
cr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false}).then(function(result){// read rows in result.rows})
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
constresult=awaitcr.allDocs({query:query_object,limit:[3,42],sort_on:[['key1','ascending'],['key2','descending']],select_list:['key1','key2','key3'],include_docs:false});// read rows in result.rows
Which returns object in the following format:
// with select_list: ['select_list_key']{"total_rows":39,"rows":[{"id":"text_id","value":{"select_list_key":"select_list_value"}},...]}// with include_docs = true{"total_rows":39,"rows":[{"id":"text_id","doc":{"key":"value"}},...]}
allDocs(options)
Returns:IQueue<IJioQueryResults> Search results.
Query for documents in the local storage. Make sure .sync() is called before.
Messages updated before this date will not be synchronized. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
syncPortalTypes
PortalTypes[]
View PortalTypes. Defines which types of messages to synchronize. If not set, all messages will be synchronized. Improves speed of synchronisation for big sets.
Yes
maxSyncObjects
number
Maximum number of objects that will be sycnrhonized from the ClearRoad platform to the local storage. Default is 1234567890.
Yes
useQueryStorage
boolean
Force using a query storage around the localStorage. Needed if the storage can not query data directly. See information on the storage.
Yes
debug
boolean
Log to console replication steps between local and remote storage.