In a Dev discussion titled “Add Calendar Event using REST API” under tags calendar-and-event and rest-api, zikapanam sought help automating calendar event creation via REST API after failing to use discourse-post-events/events.json. simon suggested embedding [event][/event] tags directly in the raw field of a topic/post creation request, emphasizing the need for a newline (\n) between tags and demonstrating with a curl example using multipart/form-data. zikapanam tried a JSON-based fetch request but received “Unprocessable Entity” — likely due to unescaped quotes and missing newline. simon clarified that quotes inside the raw string must be escaped (e.g., \") and the newline must be present. Finally, zikapanam discovered the event name requires at least 5 characters and that the API user lacked permission to create events — granting admin rights resolved the issue. The solution hinges on correct syntax, escaping, and user permissions.
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.
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]'
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.