# Add Calendar Event using REST API

**URL:** https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612
**Category:** Development
**Tags:** events, rest-api
**Created:** [May 11, 2024, 12:32am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612 "2024-05-11T00:32:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zikapanam](https://avatars.discourse-cdn.com/v4/letter/z/94ad74/32.png) [@zikapanam](https://meta.discourse.org/u/zikapanam)
#### Post date: [May 11, 2024, 12:32am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/1 "2024-05-11T00:32:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [May 11, 2024, 3:25am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/2 "2024-05-11T03:25:29Z")

</div>

How about just [create a topic or post](https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM) 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:

```plaintext
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]'

```

---

<div class="post-metadata">

### Author: ![zikapanam](https://avatars.discourse-cdn.com/v4/letter/z/94ad74/32.png) [@zikapanam](https://meta.discourse.org/u/zikapanam)
#### Post date: [May 11, 2024, 9:11am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/3 "2024-05-11T09:11:36Z")

</div>

```plaintext
    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:

```plaintext
[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.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [May 11, 2024, 9:30am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/4 "2024-05-11T09:30:12Z")

</div>

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

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

```

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

---

<div class="post-metadata">

### Author: ![zikapanam](https://avatars.discourse-cdn.com/v4/letter/z/94ad74/32.png) [@zikapanam](https://meta.discourse.org/u/zikapanam)
#### Post date: [May 11, 2024, 11:22am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/5 "2024-05-11T11:22:27Z")

</div>

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 !

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 10, 2024, 11:23am UTC](https://meta.discourse.org/t/add-calendar-event-using-rest-api/307612/6 "2024-06-10T11:23:23Z")

</div>

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