API usage examples

cURL

Example using cURL from the command line:

$ curl -u YOUR_API_KEY: https://kantree.io/api/1.0/me

Example using custom Javascript client

class KantreeClient {
    constructor(apiKey) {
        this.apiKey = apiKey;
    }
    execute(method, url, data, paginatedResults) {
        let options = {
            method,
            mode: "cors",
            headers: {
                "Content-Type": "application/json; charset=utf-8",
                "X-Api-Key": this.apiKey
            }
        };
        if (data && Object.keys(data).length) {
            if (method === "POST") {
                options['body'] = JSON.stringify(data);
            } else if (method === "GET") {
                url += "?" + Object.entries(data).map(([k, v]) => k + "=" + encodeURIComponent(v)).join("&");
            }
        }
        return fetch(`https://kantree.io/api/1.0/${url}`, options).then(r => {
            const nextPage = r.headers.get('X-Kantree-NextPage');
            return r.json().then(data => {
                if (paginatedResults) {
                    data = paginatedResults.concat(data);
                }
                if (nextPage) {
                    return this.execute(method, url, Object.assign({}, data, {page: nextPage}), data);
                }
                return data;
            });
        });
    }
    get(url, data) {
        return this.execute("GET", url, data);
    }
    post(url, data) {
        return this.execute("POST", url, data);
    }
    put(url, data) {
        return this.execute("PUT", url, data);
    }
    delete(url, data) {
        return this.execute("DELETE", url, data);
    }
}

(async () => {

    const client = new KantreeClient("00a8127ade7740151fafe6ebc858c60e66489a50");

    function formatCardObj(card) {
        let fieldValues = {ref: card.ref, title: card.title};
        card.attributes.forEach(attr => {
            fieldValues[attr.name] = attr.value;
        });
        return fieldValues;
    }

    // list all projects in orgs
    const orgId = YOUR_ORG_ID;
    let projects = await client.get(`organizations/${orgId}/projects`);
    console.table(projects.map(project => {
        return {id: project.id, title: project.title};
    }));

    const projectId = YOUR_PROJECT_ID;
    const project = projects.find(p => p.id === projectId);

    // list all cards in a project
    let cards = await client.get(`projects/${project.id}/cards`);
    console.table(cards.map(formatCardObj));

    // do a search in your project
    cards = await client.get(`projects/${project.id}/cards`, {filter: "status != done"});
    console.table(cards.map(formatCardObj));

    // create a new card: card are created under a parent card
    // all projects have a top level card that is automatically created
    // to create a card at the top level of a project, use this top level card as the parent
    let newCard = await client.post(`cards/${project.top_level_card_id}/children`, {
        title: "my new card",
        attributes: {
            "Description": "hello world"
        }
    });
    console.log(formatCardObj(newCard));

    // create a comment
    await client.post(`cards/${newCard.id}/comments`, {message: "my comment"});

    // retreive all activities, comments and logs
    let stream = await client.get(`cards/${newCard.id}/stream`);
    console.log(stream);

    // modify the title of a card
    await client.put(`cards/${newCard.id}`, {title: "my card card with new title"});

    // modify an attribute
    await client.post(`cards/${newCard.id}/attributes/Due Date`, {value: "2019-01-01"});

    // get a card
    console.log(formatCardObj(await client.get(`cards/${newCard.id}`)));

})();

Swagger client in Javascript

Example using the Swagger client:

<html>
    <head>
        <script src="https://cdn.rawgit.com/swagger-api/swagger-js/2.x/browser/swagger-client.min.js"></script>
        <script>
            var client = new SwaggerClient({
                url: "https://kantree.io/api/1.0/spec.json",
                authorizations: {
                    api_key: new SwaggerClient.PasswordAuthorization(YOUR_API_KEY, "")
                },
                success: function() {
                    client.setSchemes(['https']); // must use https
                    client.user.get_info({}, function(info) {
                        // ...
                    });
                }
            });
        </script>
    </head>
    <body>
    </body>
</html>