API: Create a category but append/add to description more details

I’m using API request to Discourse to create a category, however in the same process I would like to add text or URL reference etc, to Category description (or is it description_text as I try to understand the properties or differences).

Note also that there might be a “robot” adding to this, so the best way…

  1. create category (working)
  2. Update category or more so append the link, but to category.description or category.description_text)?

However there are no properties on update except name, color, text_color, so are there other properties I can send? Like (and which) description or/maybe description_text?

All I want to do is add a slug link to a site related to the category I’m creating… or simply add at the end of the Discourse Category description “[link][https/example.projectxxx”

1 Like

Hi, Andrew.

Although the docs only list those 3 properties, most of the properties mentioned in the Response object can be set on the create call. I found this out when I needed to script the creation of a large number of categories with the same settings except for the name and slugs.

The only property I needed to set and consistently had issues with is the permissions.

2 Likes

Yes, I’ve noticed that properties allowed are not those defined in the API documentation.

However to set the Category “description” which is very confusing as the category description is not at all what is displayed, in fact below the category name is the HTML element “cooked” which is empty, so how to set that…

From what I’ve been able to understand, I must set a post in the “About the --category name-- …” and it must be the first post in that category, but I think there is other critaria, like the cooked or also the fact that we have a Bot that first posts there.

I’ve tried adjusting the created_at/updated_at dates to make my post appear above so that the category will grab or use that description, but no luck! There must be something I’m not understanding about the “cooked” and/or category description. Very confusing and I’ve found no docs, but I’ll keep looking, if anyone has ideas on what to try, please let me know.

Hi, digging up this old topic…
Is there any way to understand what the difference between description_text and description is?
I mean except finding it out by doing :slight_smile:
maybe @alank found it out by now!
Thanks!

1 Like

If it’s not documented elsewhere, searching within the spec files in the discourse repository will answer things like this with a basic understanding of how programming languages work.

In this case, the following lines in the category spec show that description_text is derived from description and immediately available after setting description.

This says to me that description is the raw string for the description and upon setting it, it gets cooked and assigned to description_text. Or description_text could be calculated when accessed but I doubt it, either way description is the one to set.

1 Like

thanks Simon, that clarifies the difference between both parameters.

However, I just tried and none of those two parameters are actually set in the category description (or the About category post). So the issue of the OP remains.

Adding: I see in the categories.json file that these description parameters are what I want, but I somehow cannot set them through the API!

If they aren’t documented for the POST request payload, you can’t rely on being able to set them like that even if it happened to work.

I suspect the thing to do is to look at the topic_url in the response after creating category, then get a single topic using the id from that URL. Look at the post_stream in the response which has an array of posts, I imagine that will have one element.

Get the id of that post and then update a single post to add your description. That should trigger the category description being updated the same as editing the post in Discourse, same first line rule.

1 Like

I was just going to post, that’s exactly what I did :slight_smile:

    ## create category
    ret_cat = client.create_category(name, color = color, **kwargs)

    ## get first post id 
    cat_id = ret_cat["category"]["id"]
    print("Cat id", cat_id)
    topics = client.category_topics(cat_id)
    first_post_id = client.topic_posts(topics["topic_list"]['topics'][0]["id"])['post_stream']['posts'][0]['id']
    ## modify first post
    ret_post = client.update_post(first_post_id, cat["about_msg"],"set up")
3 Likes