Adding labels using the API

I’m using the API (v0.23.0) via Python’s requests library and running into an issue with adding labels to tasks. The API spec says I should be passing a Label type object, the same returned by the /labels endpoint.

Retrieving a label with:

    def get_label(self, label_id):
        return get(f"{self.base_url}/labels/{label_id}", headers=self.headers).json()

Seems to correctly fetch a label:

{'id': 10, 'title': 'Project', 'description': '<p></p>', 'hex_color': '45ff24', 'created_by': {'id': 1, 'name': '----', 'username': '----', 'created': '2022-10-10T21:35:16Z', 'updated': '2023-05-25T15:48:42Z'}, 'created': '2024-02-25T21:30:27Z', 'updated': '2024-02-25T21:30:34Z'}

However creating new tasks or trying to update tasks all seem to not result in updating the label. Creating the tasks with the label present creates the task, but no labels. Similar for updating; all info is updated except the labels. I’ve tried passing an array of Label like objects (json returned from /labels/), label id’s, label titles and a dict consisting of created_at (tried label and task dates) and label_id as specified in the /tasks/{task_id}/labels endpoint. Nothing is showing in the labels_tasks table either, except those pairs I manually create in the UI.

Logs in the backend seem to only show the request success and I’m not sure where else I can debug.

To add a label to a task, you need to use a seperate endpoint. Updating it via the task update endpoint is not supported.

A request like this one should work:

curl 'https://try.vikunja.io/api/v1/tasks/13/labels' -X PUT -H 'Content-Type: application/json' -H 'Authorization: Bearer $BEARER' --data-raw '{"label_id":1}'

This would add the label with the ID 1 to task 13.

To find out the request the frontend is doing, the web developer tools are probably helpful.

Excellent, thanks for the point out on using dev tools. I’m a junior developer and the time I have isn’t in web dev so I wouldn’t have thought about that. Using the update labels endpoint does work, not sure why it didn’t in my previous testing. Maybe sending it as parameters in a put? Either way, it’s good now.

Is there support for adding the label at task creation time via /projects/{id}/task or do you have to update after the task has been created? The API has documentation for it

Currently the api does not support that. The docs look like that does work but the problem here is the route just references the general model.

Thanks for the clarification, makes sense.

Great product, thanks for it and all the support.