View Confluence Page Images via API
Asked by Daniel_Holmes:

We are currently using the Confluence Cloud API to retrieve pages and display them to users logged in to our website. We can retrieve and display all the content without a problem, except that the images aren't being displayed. I understand that this is because an active (logged in) Confluence session is required to be able to view the images.

Our users logged into our website do not have Confluence accounts. Is there a way for these users to view the images without having to be logged in to Confluence? Is it at all possible using the API?


Answers:
author: Stephen Sifers
body: Hello Daniel,

Ideally, you're going to want users to access Confluence to see the content (either being authenticated or anonymous). There are workarounds, but they're not always supported when something breaks or changes.

The workaround would be to use another endpoint to get the contents of the attachment themselves and then display this content. You may have to add an additional step to store the attachment external to Confluence to allow viewing. Doing this will require the user of the following endpoint:

[GET /wiki/rest/api/content/{id}/child/attachment](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-get)

As stated before, this may not be a permanent solution due to possible future changes within the API or the products.

Regards,  
Stephen Sifers

comments: author: Connor B?r
body: Hello Daniel \& Stephen,

My team is currently working on a similar project: we're building a custom frontend for public documentation pages that are stored in Confluence.

I've played around with the API endpoint that Stephen suggested ([GET /wiki/rest/api/content/{id}/child/attachment](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-get)), but it only seems to return JSON metadata, not the file content itself. Am I missing something here? Could you share a sample query to fetch the file itself?

I've also tried to reverse engineer Atlassian's asset API ([api.media.atlassian.com](https://api.media.atlassian.com)), but couldn't figure out how to successfully authenticate with it from the server.

Our plan is to proxy asset requests from our frontend through our server (which handles authentication) to Atlassian. This way, we can ensure that the assets remain secure by default while making some of them accessible to the public.

Any tips and hints are much appreciated!


author: Daniel_Holmes
body: Hi Connor,

You will need to do an additional request to download the file. You can find the file links in the JSON metadata in the response from [GET /wiki/rest/api/content/{id}/child/attachment](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-get). Below is an implementation in Python using the requests library. Let me know if it helps:

```
attachments_url = f"https://yoursite.atlassian.net/wiki/rest/api/content/{id}/child/attachment"
auth = (CONFLUENCE_API_USER, CONFLUENCE_API_KEY)
response = requests.get(attachments_url, auth=auth)
data = response.json()
file_data = data['results']

for file in file_data:
    download_url = "https://yoursite.atlassian.net/wiki" + file['_links']['download']
    response = requests.get(download_url, auth=auth)
    image_file = ContentFile(response.content)
```


author: MrHankey
body: Hi [@Daniel_Holmes](/t5/user/viewprofilepage/user-id/1848477)   

thank you for your code snippet. I just wanted to ask you the following question:

What happens to the image_file? I mean... sure you got the file but then ..? I could also consider saving those ContentFiles in an array and passing it to the frontend. But how can we achieve to combine the page content with the images, without making the (unrecommended) workaround Stephen already suggested?

Thank you in advance!


Tags: atl-adm

cloud

confluence-cloud

rest-api