Add Calendar Event using REST API

Hello,

I’ve been searching for hours how to use REST API to add calendar event to a topic and unfortunately, i couldn’t find an answer.

I’ve created a category calendar and could display my topic event created manually but could not handle to automate the topic event creation when i add a topic using REST API.

I’ve found “discourse-post-events/events.json” but when i tried this API, i get “Not Found”. I suppose it’s because there is the need for event ids and i don’t know how to create them.

Thanks for your help, advice and comments.

1 Like

How about just create a topic or post for the event via the API and add the [event][/event] tags to the post as the value of its raw parameter?

The trick seems to be that there has to be a newline character (\n) between the opening and closing event tags.

I’m using "Content-Type: multipart/form-data" in the example below because it’s easier than dealing with JSON from the command line. Also $api_key is set to the value of an actual API Key:

curl -X POST "http://localhost:4200/posts.json" \
-H "Api-Key: $api_key" \
-H "Api-Username: scossar" \
-H "Content-Type: multipart/form-data" \
-F title="Can I create an event via the API?" \
-F category=6 \
-F raw='[event start="2024-05-10 20:00" status="public" timezone="America/Vancouver" end="2024-05-10 21:30" allowedGroups="trust_level_0"]\n[/event]'
1 Like
    const url = DISCOURSE_BASE_URL + '/posts.json';
    const message = {
        method: 'POST',
        headers: {
            'Api-Key': DISCOURSE_API_KEY,
            'Api-Username': DISCOURSE_API_USERNAME,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "title": title, 
            "raw": content,
            "category": categoryId
        })
    };
    const response1 = await fetch(url, message);

I get “Unprocessable Entity” when raw as follow:

[event start="2024-05-16 19:00" status="public" name="585" timezone="Europe/Paris" allowedGroups="trust_level_0" end="2024-05-16 21:00"]
[/event]

When i get off event declaration and put alternative description, my forum gets updated but as soon as i put event declaration, i get Unprocessable Entity.

I don’t understand what i’m doing wrong.

You’ll need to escape the double quotes that occur within the string. For example:

"raw": "[event start=\"2024-05-16 19:00\"...

Also, make sure to include the newline character (\n) between the event tags.

I took my data and tried to create the post using the UX. It said me that the event name must be 5 characters minimum.

After that i try your curl query @simon and it said something interesting : I cannot create post because i have not the rights to create events …

So i gave him the admin rights.

And now it’s ok. Every is working.

Thanks for your help !

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.