Need help to insert itens with API

I’m trying for a lot of days and it doesn’t work.:persevere: What I want to do is insert items to Cockpit through API and not one by one manually.
I can see the import function with a json file and works fine, but I need to insert with a form and post method.
Is there any tutorial? I really need it.
Thank you!


I guess you are trying to insert data into a collection.

Short answer to your problem: how you insert data is described within the documentation:

So what yo need is:


Create / Update collection entries

fetch('/api/collections/save/posts?token=xxtokenxx', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        data: {...}
.then(entry => console.log(entry));

So what you need first of all is a working API key.

Either you use a user’s API key (of course that user needs to have write access to the collection you want to fill (but then be carefull when using JS ajax calls (for the key is exposed to the client then))), or set public write access to the collection (!!!what is even more dangerous and absolutely NOT recommented!! only do this for testing or better even forget that you just read this !!!) or create a dedicated API key (through the cockpit settings) that allows you to access your desired collection.

If you got this you can just send ajax (post) requests like this to insert data into your collection:

$.post('http://your-cockpit-installat.ion/api/collections/save/posts?token=YOURWORKINGTOKEN', {
  "data" : {
    "name" : "chris",
    "birthdate" : "01.02.2000",
    // and so on
}, function(res) {})

or if you want to use PHP: this is the one method from my cockpit helper that is responsible for inserting or updating data (it should explain what you have to do, even if there are references on class vars within)

     * insert OR update a collection entry
     * @param string $collection the collection name
     * @param Array $data the date fields that shall be set to the collection
     * @param string|null $id ONLY IF A ENTRY SHALL BE UPDATED: the ID of the collection entry that shall be updated. Null if you want to create a new entry
     * @param string|null $apiKey the apikey for this request or null if the helper apikey shall be used (of course therefor it must be configurated)
     * @return Array the result
    public static function saveEntry($collection, $data = [], $id = null, $apiKey=null) {
        $url = self::$_COCKPIT_BACKEND_URL . '/api/collections/save/' . $collection . '?token=' . ( $apiKey ? $apiKey : self::$_COCKPIT_RESTAPI_TOKEN );
        if($id) $data['_id'] = $id;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
            'data' => $data
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        return curl_exec($ch);

Hi there! Yes, I am trying to insert data into a collection. I got a API key and I’ve understand the concept about access. All what I want to do is a public list with json to list into a simple HTML page. What a really really want is a easy way to only me can insert data to my cockpit.

I understood your point of view with code but I can’t do the same.:disappointed_relieved: I don’t need complex ajax or php (even that I understand your php code, I don’t know how to code this).

I don’t wanna be a jerk, but is there any simple code or tutorial that you can sent me?

Thank you so much!!

I don’t know how we could break it down to something even more simple - I don’t even think that this is possible.

The posted examples are the absolute very minimalistc basic on cockpit CRUD operations.

What could be more simple then just send one single ajax request to the backend in order to inset some new data?

This will insert a new data row into your collection:

$.post('http://your-cockpit-installat.ion/api/collections/save/posts?token=YOURWORKINGTOKEN', {
  "data" : {
    "name" : "chris",
    "birthdate" : "01.02.2000",
    // and so on
}, function(res) {})

what is so complicated about it?

I’m so sorry for being a newbie. I want to learning more and I’m studying for that.
What I know is a bit PHP, Bootstrap and CSS. I’ve tried to insert data into collection through a PHP form, setting my action parameter to the cockpit url with token and method post.
I found an example to transform my post data to a json, following your example, but it still doens’t work. I followed so many tutorials to deal with ajax post, but none of them worked too.

Well I guess if you don’t know the very basics of web developement this messageboard is the wrong place for getting help. We can help you understand how cockpit works and how to reach your goals according to cockpit but we are not here to teach how webtechnologies work. :confused:

I’m gonna keeping trying until it works. Thank you.

I want to say really really thank you! Finally I can insert with api!!
I just paste your code with fetch into Google Chrome console and work! Now I have to learn how to insert itens without console.

this is how you do it using javascript / without console. You just need jQuery for this
Getting familar with jquery is always a good idea - most sites use jquery for just everything

Please make sure you have the headers set “Content-Type: application/json” #sending json formatted data

Sending the Data with php curl but i only get an 404 error why is that happending?
Any idea? I’m pretty sure to change posts with my own collection name :wink:

Care to share your curl command?

After created this post and still receiving notifications I can see all my questions was from a newbie and I’m feel very happy to understand all question that I didn’t understand two years go.
Keep learning! :sunglasses:

1 Like