How to get space ID on the UI or how to utilise space key in v2 API?

Dániel Szajcz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 24, 2024

Hello Community,

I am building a portal that allows users to see details regarding their Confluence spaces.

My plan is to use the Confluence Cloud REST API v2, and I have found the following endpoint that could help my case: Get space by id. The problem is that the endpoint requires space IDs, that I did not manage to find on the web UI.

Also, our users are familiar with the Confluence space key and it would be great if we could utilise the space key, but the linked endpoint only accepts integers as IDs. I found that the v1 API still works with space keys, but I can see that the endpoint is deprecated.

Therefore, I am interested in two things:

  • Is there a way for Confluence Cloud users to find the space IDs on the web UI?
  • Is there an endpoint in the v2 API that I missed and allows me fetch space details using space keys?

Thanks a lot in advance!

1 answer

2 votes
Adriana Valentina Hernández Moreno
Contributor
August 26, 2024

Hello Dániel, 

I was looking for the same API endpoint and noticed that most calls utilize space ID instead of space key. So I created a simple python3 script to get the space ID given a specific space Key:

import requests
from requests.auth import HTTPBasicAuth
import json


site = "https://<YOUR_SITE_NAME>.atlassian.net/"
auth = HTTPBasicAuth("<YOUR_EMAIL>", "<YOUR_SITE_NAME>")


headers = {
  "Accept": "application/json"}


next_page = "wiki/api/v2/spaces/"


listSpace = {} #initialize dictory 


while next_page: 
    url = site+next_page
    response = requests.request(
      "GET",
      url,
      headers=headers,
      auth=auth
    )
    if response.status_code == 200:
      data = response.text
      space_data = json.loads(data)
      #pagination
      if 'next' in space_data['_links']:
        next_page = space_data['_links']['next']
      #getting data for the dictionary
      for x in space_data['results']:
        spacekey = x['key'] #Getting all space keys
        spaceid = x['id'] #Getting all space ids
        listSpace[spacekey]=spaceid
      else:
        break


key = input("What is the spacekey? \n") #Asks the user for the space key.
print(listSpace.get(key))
             
             

I hope this helps even though it does not answer your questions,

Best Regards,

Adriana

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events