Unable to update Category Setting thru API

Hello, we’ve been working on a tool to create categories using a CSV input. We were able to successfully establish the parent-child relationships while creating the categories. However, we’re encountering issues when it comes to applying category settings—it consistently fails. Any guidance on how to apply category settings (like the ones listed below) after creating a new category would be appreciated. We’ve even tried adding a delay of up to 40 seconds before applying the settings, but it’s still not working.

Also want to Update Category Description :: update_about_post(category_id, description) # Update description only.

Also I wanted to add two additional custom field for Category , how to add this .

Function to update the category settings

def update_category_settings(category_id):
time.sleep(5) # Add a delay to ensure the category is fully created before applying settings

url = f"{DISCOURSE_URL}/categories/{category_id}"
data = {
    "subcategory_list_style": "boxes",  # Set subcategory style to "Boxes"
    "show_subcategory_list": True,  # Show subcategory list above topics
    "allow_solved_on_category": True  # Enable solved feature
}

response = requests.put(url, json={"category": data}, headers=headers)

if response.status_code == 200:
    print(f"Category settings updated for category ID {category_id}")
else:
    print(f"Failed to update settings for category ID {category_id}: {response.status_code}, {response.text}")

Any input would be appreciated as I am stuck at this point…

I haven’t tested it with Python, but it would be worth trying to use the string "true" instead of the boolean True for the boolean parameters. Discourse will interpret the strings "true" and "false" as booleans.

I’m assuming you are able to get some API calls to work, and are just having trouble with this particular one. Let us know if that’s not the case.

Thanks Simon …you are absolutely correct, I am able to get some API calls to work…I am able to create category with parent and child relationship…its just that I am not able to Add Description details, and other Category related settings unable to make it work.

I tried updating some category settings on my local forum from the command line. This works:

❯ curl -X PUT "http://localhost:4200/categories/10.json" \
      -H "Api-Key: $api_key" \
      -H "Api-Username: system" \
      -H "Content-Type: multipart/form-data;" \
      -F "show_subcategory_list=true" \
      -F "subcategory_list_style='boxes'" \
      -F "allow_badges=false"

I’d try using "true" instead of True if you can’t get it to work.

I think that’s expected. To add the description you need to edit the category’s “about” topic. Unfortunately, I don’t think the “about” topic URL is returned from a request to the category URL (for example http://localhost:4200/categories/10.json). You can get it from your site’s categories/json URL though. The field that contains the “about” topic URL is called topic_url. For example: "topic_url":"/t/about-the-general-category/3".

So unless there’s an easier way I’m not seeing, you’ll need to make a request to /categories.json, get the topic_url for the category you’re interested in, then make an API request to edit that topic’s first post.