# Create new collection over API

**URL:** <https://discourse.getcockpit.com/t/create-new-collection-over-api/2037>\
**Category:** Support\
**Created:** [August 9, 2021, 10:07am UTC](https://discourse.getcockpit.com/t/create-new-collection-over-api/2037 "2021-08-09T10:07:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snewmn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/snewmn/32/991_2.png) [@snewmn](https://discourse.getcockpit.com/u/snewmn)\
**Post date:** [August 9, 2021, 10:07am UTC](https://discourse.getcockpit.com/t/create-new-collection-over-api/2037/1 "2021-08-09T10:07:38Z")

</div>

Hi, is it possible to dynamically create new collections from the API?

I would like each of these collections to have the exact same fields as each other. Ive seen this post [here](https://discourse.getcockpit.com/t/create-update-collection/1543), but I think thats for existing collections

For example:

```auto
fetch('example.com/api/collections/updateCollection/posts?token=xxtokenxx', {
        method: 'post',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            data: {
            fields: [
                {
                    name: "display_name",
                    label: "Display Name",
                    type: "text",
                    default: "",
                    info: "Name of the country as displayed to customers",
                    group: "",
                    localize: false,
                    options: [],
                    width: "1-1",
                    lst: true,
                    acl: [],
                    required: false
                }
            ]
          }
        })
    })
    .then(collection => collection.json())
    .then(collection => console.log(collection));

```

Throws a 404 error since the collection in this example called ‘posts’ doesn’t already exist. So my question is, is there a way to make it so the above would create a whole new collection type? Or is there a web hook I could try use?

---

<div class="post-metadata">

**Author:** ![abernh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/abernh/32/859_2.png) [@abernh](https://discourse.getcockpit.com/u/abernh)\
**Post date:** [August 9, 2021, 10:35am UTC](https://discourse.getcockpit.com/t/create-new-collection-over-api/2037/2 "2021-08-09T10:35:57Z")

</div>

The endpoint you are looking for is

`/api/collections/createCollection`

it expectes the params

```auto
 name string
 data [
            'name' => $name,
            'label' => $name,
            '_id' => $name,
            'fields' => [],
            'sortable' => false,
            'in_menu' => false,
            '_created' => $time,
            '_modified' => $time
        ]

```

But it is only available for users within the `admin` group. And that means you have to use a granting-all-access-token … just make sure this token is not accessible by anybody but your backend.

> <https://github.com/agentejo/cockpit/blob/568b0124352f6d27df359e8c19a70d2dd1961e87/modules/Collections/Controller/RestApi.php#L254>

---

<div class="post-metadata">

**Author:** ![snewmn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/snewmn/32/991_2.png) [@snewmn](https://discourse.getcockpit.com/u/snewmn)\
**Post date:** [August 9, 2021, 10:56am UTC](https://discourse.getcockpit.com/t/create-new-collection-over-api/2037/3 "2021-08-09T10:56:13Z")

</div>

Thanks for that! couldn’t find in docs!
