Create new collection over API

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, but I think thats for existing collections

For example:

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?

The endpoint you are looking for is

/api/collections/createCollection

it expectes the params

 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.

1 Like

Thanks for that! couldn’t find in docs!