API to read/write tasks in Confluence?
Asked by Philip Colmer:

I've been asked to try and write a script to synchronise tasks between Confluence and Google. However, I can't find an API to read and/or write tasks in Confluence. Is there one?

Thanks.


Answers:
author: Diego
body: Hello there Philip!

You sure can do this with Confluence REST API. For this to work we need to send a curl with the necessary content into your instance. For example:

```
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"MY TASK PAGE","space":{"key":"CIAS"},"body":{"storage":{"value":"<p>This is a new page</p> <ac:task-list><ac:task><ac:task-status>incomplete</ac:task-status><ac:task-body>task list item</ac:task-body></ac:task></ac:task-list>","representation":"storage"}}}' http://yourconfluence.address/rest/api/content/ | python -mjson.tool
```

This is the resulting page after sending this curl to my local Confluence instance:

![Screen Shot 2019-01-23 at 18.06.40.png](https://community.atlassian.com/t5/image/serverpage/image-id/43412iFBEB67606004F521/image-size/large?v=v2&px=999 "Screen Shot 2019-01-23 at 18.06.40.png")

You need to run this **curl command** from a terminal, either Linux or macOS. This also works with Windows but you will need to install some software before being able to.

For further information on curl, you can check here:

**[How to cURL POST from the Command Line](http://osxdaily.com/2017/01/30/curl-post-request-command-line-syntax/)**

**[Installing and using cURL](https://develop.zendesk.com/hc/en-us/articles/360001068567-Installing-and-using-cURL)**

**[15 Tips On How to Use 'Curl' Command in Linux](https://www.tecmint.com/linux-curl-command-examples/)**

What happens here is that you are **POSTing to** **the server** the page **body** as **storage format**string. With storage format, you can create structures using a simple syntax:

**[Confluence Storage Format - Lists](https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html#ConfluenceStorageFormat-Lists)**

There is much more that you can do with REST API! You can check some working examples here:

**[REST API Examples](https://developer.atlassian.com/cloud/confluence/rest-api-examples/)**

Knowing that, it is now a matter of choosing how you want to trigger this curl when tasks are added/removed.

Let us know if this does help your objective!

comments: author: Philip Colmer
body: Hi Diego

Thanks for this. It helps me understand how to add a task to an existing page but, unfortunately, it doesn't help me with retrieving tasks from Confluence.

I was hoping that since there is a My Tasks page (which clearly does have a way of pulling all of the tasks together) that there might be a programmatic way of achieving that outcome.


author: Diego
body: Hello there Philip!

You can also retrieve the page contents with **REST API**. The cURL needed would look like this:

```
curl -u admin:admin http://yourinstance.address/wiki/rest/api/content/<contentID>?expand=body.storage | python -mjson.tool
```

You can check more information about REST API here:

[**REST API Examples - Read content, and expand the body**](https://developer.atlassian.com/cloud/confluence/rest-api-examples/?_ga=2.98603741.275380391.1548076797-1770418407.1541684782#read-content-and-expand-the-body)

You could set a **cron job** that calls the cURL to read a page content and then trigger another job to call the cURL to update said page.

**[How to Schedule tasks on Linux](https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/)**

The trickier part is when we need to **compare both pages**, from Google and Confluence. We can do that in Linux by comparing the Strings:

**[String Comparison](https://bash.cyberciti.biz/guide/String_comparison)**

After you compare them, you can send the results to a variable and use it with the cURL to update the page. Much like how a payload would work.

Also, I would like to clarify that the page named MY TASK PAGE was set just as a placeholder in my last example.

Hope this helps out!


author: Philip Colmer
body: Hi Diego

I appreciate that ... although that would require me to scan every page in every space, something that the My Tasks page doesn't seem to need to do.

I will take it, therefore, that there isn't a cleaner way to retrieve tasks for an individual.

Thanks for the assistance.

Regards

Philip


author: Diego
body: Yes, this requires you to scan for every page that has tasks in it. However, **each time you publish a page** , a new **database entry** is **generated** in the **CONTENT table** in Confluence. Each entry has a **different contentid** value.

So, if you end up editing one of those pages with tasks, you will need to get their contentid again. But, as long as you keep the page name unique to your entire instance, you should be able to find it relatively easily by querying the database and matching the **lowertitle** column.

We have a similar situation where a customer needed to find a specific page version but the query should help you to build yours for this case:

**[Get required version of content properties via REST API](https://community.atlassian.com/t5/Confluence-questions/Get-required-version-of-content-properties-via-REST-API/qaq-p/983642)**

But yes, you are right. There is no real "clean" way of doing this. No native method or API is currently capable of doing this kind of integration and comparisons.


author: David
body: Hi, [@Philip Colmer](/t5/user/viewprofilepage/user-id/510078)

FYI, I also need the API to read the task list and I got the answer form here [https://community.atlassian.com/t5/Confluence-questions/Listing-user-tasks-through-REST-API-doesnt-always-work/qaq-p/1459293?utm_source=dm\&utm_medium=unpaid-social\&utm_campaign=P:online\*O:community\*I:social_share](https://community.atlassian.com/t5/Confluence-questions/Listing-user-tasks-through-REST-API-doesnt-always-work/qaq-p/1459293?utm_source=dm&utm_medium=unpaid-social&utm_campaign=P:online*O:community*I:social_share)


author: Ilnur Gabdushev
body: Have same issue - can't get task list via API.

[@Diego](/t5/user/viewprofilepage/user-id/1188952) I've tried your advice, but it doesn't help me. Details follows.

As you said, I've created a page with macro on it. I've done it manually via web-ui (browser), not REST API but I think it does not matter, because when I access this page via web-ui - it shows me a list of all inline tasks - i.e. works as expected.

But when I request this page via REST API as you said

```
/rest/api/content/<contentID>?expand=body.storage
```

it respond with

```
{
  "id": "60265808",
  "type": "page",
  "status": "current",
  "title": "tasks all",
  "body": {
    "storage": {
      "value": "<p><ac:placeholder>Edit the Task Report macro to customise your report.</ac:placeholder></p><p><ac:structured-macro ac:name=\"tasks-report-macro\" ac:schema-version=\"1\" ac:macro-id=\"fdd20554-fe0a-424f-9a52-53fd7a6b8bbf\"><ac:parameter ac:name=\"reverseSort\">true</ac:parameter><ac:parameter ac:name=\"status\">complete</ac:parameter></ac:structured-macro></p>",
      "representation": "storage",
      "_expandable": {
        "content": "/rest/api/content/60265808"
      }
  },
//...
```

i.e. - It returns macro markup, but not the result of its execution!

So how do i get inline tasks list?

For other who have same issue:

Please vote for feature request [CONFSERVER-56023: REST API for inline-tasks](https://jira.atlassian.com/browse/CONFSERVER-56023)

comments: author: Ilnur Gabdushev
body: Found that this *expand=body.storage* in

```
/rest/api/content/<contentID>?expand=body.storage
```

Must be *expand=body.export_view*:

```
/rest/api/content/<contentID>?expand=body.export_view
```

Then value will contain HTML with result.


Tags: confluence-server

server