The trm-api
package is a wrapper to simplify GET requests and JSON response parsing from the Tasa Representativa del Mercado API.
npm install trm-api
The TrmApi
class provides five methods: latest()
, between(options)
, history(options?)
, date(date)
, and query(query)
.
const TrmApi = require("trm-api").default;
const trmApi = new TrmApi();
import TrmApi from "trm-api";
const trmApi = new TrmApi();
The module is written in TypeScript and type definitions files are included.
A limited number of requests can be made without an app token, but they are subject to much lower throttling limits than requests that do include one.
With an app token, your application is guaranteed access to it's own pool of requests.
You can pass your app token in the constructor:
const trmApi = new TrmApi("YOUR-APP-TOKEN-HERE");
Provides the most recent quote:
trmApi
.latest()
.then((data) => console.log(data))
.catch((error) => console.log(error));
The response is a single object with the latest information from the Tasa Representativa del Mercado API:
{
valor: '3792.98',
unidad: 'COP',
vigenciadesde: '2020-08-05T00:00:00.000',
vigenciahasta: '2020-08-05T00:00:00.000'
}
Returns an array with the quotes between two dates: startAt
and endAt
.
The options
argument is an object with the following fields:
Field | Type | Description |
---|---|---|
startAt | Required | The initial date of the data to be retrieved in YYYY-MM-DD format. |
endAt | Required | The final date of the data to be retrieved in YYYY-MM-DD format. |
order? | Optional | Can be 'ASC' or 'DESC' . Defaults to 'ASC' . |
trmApi
.between({ startAt: "2020-07-02", endAt: "2020-07-7", order: "DESC" })
.then((data) => console.log(data))
.catch((error) => console.log(error));
Will return the following array:
[
{
valor: "3633.32",
unidad: "COP",
vigenciadesde: "2020-07-07T00:00:00.000",
vigenciahasta: "2020-07-07T00:00:00.000",
},
{
valor: "3645.90",
unidad: "COP",
vigenciadesde: "2020-07-04T00:00:00.000",
vigenciahasta: "2020-07-06T00:00:00.000",
},
{
valor: "3660.18",
unidad: "COP",
vigenciadesde: "2020-07-03T00:00:00.000",
vigenciahasta: "2020-07-03T00:00:00.000",
},
{
valor: "3723.67",
unidad: "COP",
vigenciadesde: "2020-07-02T00:00:00.000",
vigenciahasta: "2020-07-02T00:00:00.000",
},
];
Returns an array with all the values starting from the most recent value.
The options
optional argument is an object with the following fields:
Field | Type | Description |
---|---|---|
order? | Optional | Can be 'ASC' or 'DESC' . Defaults to ASC . |
limit? | Optional | Maximum number of results to return. Defaults to 1,000. Maximum of 50,000. |
trmApi
.history({ limit: 30 })
.then((data) => console.log(data))
.catch((error) => console.log(error));
Returns the TRM for an specific date given in YYYY-MM-DD
format.
trmApi
.date("2020-08-09")
.then((data) => console.log(data))
.catch((error) => console.log(error));
The response is a single object with the information for the given date:
{
valor: '3769.67',
unidad: 'COP',
vigenciadesde: '2020-08-07T00:00:00.000',
vigenciahasta: '2020-08-10T00:00:00.000'
}
This method allows you to use any custom SoQL query
.
For example:
trmApi
.query(
"SELECT valor, vigenciadesde WHERE valor >= 4150 AND vigenciadesde < '2020-08-01'"
)
.then((data) => console.log(data))
.catch((error) => console.log(error));
It will return an array of objects each with the requested fields (or all the fields if no SELECT clause used):
[
{
valor: "4153.91",
vigenciadesde: "2020-03-20T00:00:00.000",
},
];
The packages provides a simple CLI to quickly get TRM quotes.
If called without arguments it will return the current exchange rate:
$ npx --quiet trm-api
3767.05
It can also be called with a given date in YYYY-MM-DD
format to get the exchange rate for that date:
$ npx --quiet trm-api 2010-09-23
1803.71
Contributions, issues and feature requests are welcome!
Give a ⭐️ if you like this project!