What can I do with Cointray's API?

All functions of Cointray are also available in its RESTful API. You can add and delete coins. You can create, delete and update repetitions, etc.

Note that there currently is a limit of five requests per user per minute for the API.

Who can use Cointray's API?

Everyone. If you are interested in using the API, just navigate to this page you are currently reading while logged in to Cointray. You will then see your API key (the "password" required to access the API) at the top of the page.

Great! But how?

As every RESTful API, you access Cointray's API using HTTP calls. This means, you can for example use a command line tool like curl or wget or access it via a programming language (for example using Python's Requests library).

The API endpoints return JSON data by default.

Browseable API Description

A browseable API description is available. If you happen to know the "Django Rest Framework" (DRF) you will feel right at home.

Using curl

The following is an example call that lists all available coins:

$ curl -s -H "Authorization: Token 41...c5" -XGET https://www.cointray.net/api/v1/things/ | python -m json.tool [ { "url": "https://www.cointray.net/api/v1/things/1/", "owner": "toor", "title": "Coiffeur", "repetition_count": 2, "most_recent_repetition": { "url": "https://www.cointray.net/api/v1/repetitions/3/", "ts": "2018-07-02T21:12:36Z" }, "most_recent_repetition_delta": " 5 months 27 days 22 minutes" }, { "url": "https://www.cointray.net/api/v1/things/3/", "owner": "toor", "title": "Zoo", "repetition_count": 1, "most_recent_repetition": { "url": "https://www.cointray.net/api/v1/repetitions/2/", "ts": "2018-06-25T14:45:10.868258Z" }, "most_recent_repetition_delta": " 6 months 4 days 6 hours" } ]

Here is a call that adds a new "day resolution only" coin:

$ curl -s -H "Authorization: Token 41...c5" \ -H "Content-Type: application/json" \ -XPOST -d '{"title": "Daily coin (API)", "day_res": true, "max_num_rep": 5}' \ https://www.cointray.net/api/v1/repetitions/ | python -m json.tool { "day_res": true, "max_num_rep": 5, "most_recent_repetition": null, "most_recent_repetition_delta": null, "owner": "toor", "repetition_count": 0, "title": "Daily coin (API)", "url": "https://www.cointray.net/api/v1/things/19/" }

Or a call that adds a repetition "now" to a specific coin:

$ curl -s -H "Authorization: Token 41...c5" \ -H "Content-Type: application/json" \ -XPOST -d '{"thing": "https://www.cointray.net/api/v1/things/2/"}' \ https://www.cointray.net/api/v1/repetitions/ | python -m json.tool { "url": "https://www.cointray.net/api/v1/repetitions/17/", "thing": "https://www.cointray.net/api/v1/things/2/", "ts": "2019-03-05T21:24:39.386637Z" }

Or a call that updates an existing repetition's time:

$ curl -s -H "Authorization: Token 41...c5" \ -H "Content-Type: application/json" \ -XPOST -d '{"ts": "2018-07-25T12:11:10Z", "thing": "https://www.cointray.net/api/v1/things/2/"}' \ https://www.cointray.net/api/v1/repetitions/17/ | python -m json.tool { "url": "https://www.cointray.net/api/v1/repetitions/17/", "thing": "https://www.cointray.net/api/v1/things/2/", "ts": "2018-07-25T12:11:10Z" }

Or a call that deletes an existing repetition:

$ curl -s -H "Authorization: Token 41...c5" \ -H "Content-Type: application/json" \ -XDELETE https://www.cointray.net/api/v1/repetitions/17/

And the same for a coin:

$ curl -s -H "Authorization: Token 41...c5" \ -H "Content-Type: application/json" \ -XDELETE https://www.cointray.net/api/v1/things/2/

Using requests

This is an example python script that interacts with Cointray. It can list a coin's repetitions or add a repetition (with the timestamp set to "now") to a specific coin.

import argparse import requests import json from datetime import datetime, timezone API_URL = "https://www.cointray.net/api/v1/" API_KEY = "9d...fc" def main(): parser = argparse.ArgumentParser() parser.add_argument("-u", "--update", help="do you want to update?", action="store_true") parser.add_argument("-c", "--create", help="do you want to create?", action="store_true") args = parser.parse_args() headers = { "Authorization": " ".join(["Token", API_KEY]), "Content-Type": "application/json" } coins_url = "".join([API_URL, "coins/"]) create_url = "".join([API_URL, "repetitions/"]) update_url = "".join([API_URL, "repetitions/42/"]) if not args.update and not args.create: r = requests.get(coins_url, headers=headers) # fetch all coins elif args.create: print("let's create!") now = datetime.now(timezone.utc) payload = { "thing": "https://www.cointray.net/api/v1/things/1/", "ts": now.isoformat(), } print("create: {0}".format(payload)) r = requests.post(create_url, headers=headers, json=payload) elif args.update: print("let's update!") now = datetime.now(timezone.utc) payload = { "thing": "https://www.cointray.net/api/v1/things/1/", "ts": now.isoformat(), } print("update: {0}".format(payload)) r = requests.put(update_url, headers=headers, json=payload) if r.status_code == requests.codes.ok: print(json.dumps(r.json(), indent=4)) else: print("Upsy daisy: {}".format(r.status_code)) print(r.text) if __name__ == "__main__": main()

What is an API?

An Application Programming Interface (API) defines how programs can interact with "something". The "something" in this case is Cointray.

The API for Cointray is what is called a RESTful API. RESTful APIs are especially handy to be used by other Web applications.