# Update collection and singleton via api

**URL:** <https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404>\
**Category:** Support\
**Created:** [November 15, 2018, 3:43pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404 "2018-11-15T15:43:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![elpeyotl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/elpeyotl/32/165_2.png) [@elpeyotl](https://discourse.getcockpit.com/u/elpeyotl)\
**Post date:** [November 15, 2018, 3:43pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/1 "2018-11-15T15:43:02Z")

</div>

Hello people

Well cockpit is great. I realy like the simplicity. I tried some things out but could find any answer to it.

According to the docs can create/update collection entries from frontend.

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

```

Well I was able to create new entries but couldnt find out how do UPDATE existing entries. The code above only creates new ones. Does anyone have a clue how to do that? And is it even possible to update singletons from frontend?

Ah, and is there a way to contribute to the documentation? Like help them creating better updated docs?

thanks.

---

<div class="post-metadata">

**Author:** ![serjoscha87](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/serjoscha87/32/151_2.png) [@serjoscha87](https://discourse.getcockpit.com/u/serjoscha87)\
**Post date:** [November 15, 2018, 10:06pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/2 "2018-11-15T22:06:56Z")

</div>

The only difference in usage between update and insert is the fact that you pass an ID along or not.

If you post with the “\_id” field set in your data, cockpit will update the data determined by the id you gave; if you post without the “\_id” field cockpit will create a fresh new entry.

* * *

If you’d like to contribute to the documentation, create a PR here: [https://github.com/agentejo/cockpit-docs](https://github.com/agentejo/cockpit-docs)

---

<div class="post-metadata">

**Author:** ![elpeyotl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/elpeyotl/32/165_2.png) [@elpeyotl](https://discourse.getcockpit.com/u/elpeyotl)\
**Post date:** [November 16, 2018, 1:24pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/3 "2018-11-16T13:24:13Z")

</div>

Thank you very much for your reply! Will try it as soon I can. Is there a way to update singletons as well from frontend? I do not see like /singletons/save in the api.

And thanks for the link with the git for the docs.

---

<div class="post-metadata">

**Author:** ![serjoscha87](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/serjoscha87/32/151_2.png) [@serjoscha87](https://discourse.getcockpit.com/u/serjoscha87)\
**Post date:** [November 16, 2018, 3:11pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/4 "2018-11-16T15:11:28Z")

</div>

the api method to call is “update\_data”:

> <https://github.com/agentejo/cockpit/blob/8302d5ed36124efee202323e79d68409bc0671fe/modules/Singletons/Controller/Admin.php#L115>

you call it this way: [http://your-domain.com/singletons/update\_data/my\_pretty\_cool\_singleton\_name](http://your-domain.com/singletons/update_data/my_pretty_cool_singleton_name)

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 19, 2018, 2:02pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/5 "2018-11-19T14:02:12Z")

</div>

> [@elpeyotl](#):
>
> Is there a way to update singletons as well from frontend? I do not see like /singletons/save in the api.

If you want to save singleton data via api, just create a custom api endpoint in  
`config/api/singletons/save/singletonname.php` with this content:

```php
<?php

return $this->invoke('Singletons\\Controller\\Admin', 'update_data', ['singletonname']);

```

Now send a request:  
`https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx`

json post body:  
`{"data":{"title":"test","content":"updated content"}}`

**Caution: It updates empty values, too. So you always have to send all fields or the other fields will be deleted.**

* * *

edit: fixed wrong folder name of custom endpoint - Thanks @SimonHayden for the correction

---

<div class="post-metadata">

**Author:** ![SimonHayden](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@SimonHayden](https://discourse.getcockpit.com/u/SimonHayden)\
**Post date:** [March 19, 2019, 2:58pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/6 "2019-03-19T14:58:08Z")

</div>

There is an error in @raffaelj’s, but otherwise is correct.

The php file `singletonname.php` should be in `config/api/singletons/save/singletonname.php`, which would then create an endpoint to save to Singleton “singletonname” at `https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx`

The API endpoint won’t work when not in `api` folder.

Equally, one could also put `singletonname.php` in `config/api/singletonname.php` and then POST data to `https://url.to/cockpit/api/singletonname?token=xxtokenxx`.

---

<div class="post-metadata">

**Author:** ![lastfuture](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/lastfuture/32/699_2.png) [@lastfuture](https://discourse.getcockpit.com/u/lastfuture)\
**Post date:** [November 13, 2020, 12:37am UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/7 "2020-11-13T00:37:52Z")

</div>

Hello there. I want to save singleton data via API, using only the API key without user credentials, but I can’t quite get this to work. When I `POST` a JSON body to my singleton save endpoint `/api/singletons/save/prepared_spells?token=xyz` (created as described in raffaelj’s answer) I get a response with code 302 Found but no body:

```
HTTP/1.1 302 Found
Date: Fri, 13 Nov 2020 00:02:00 GMT
Server: Apache/2.4.41 (Ubuntu)
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding, Cockpit-Token
Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE
Access-Control-Expose-Headers: true
Location: /auth/login?to=/api/singletons/save/prepared_spells
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

```

The data in the singleton isn’t updated.

The header response `Location: /auth/login?to=/api/singletons/save/prepared_spells` makes me believe it wants a user auth, but I would like this particular singleton to be updateable without user credentials, with only an API token.

Any pointers on what I need to adjust to get this to work would be appreciated.

---

<div class="post-metadata">

**Author:** ![meqmeq](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@meqmeq](https://discourse.getcockpit.com/u/meqmeq)\
**Post date:** [March 7, 2022, 4:26pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/8 "2022-03-07T16:26:35Z")

</div>

Hmm I tried this but I’m getting this error,

```auto
localhost:9001/api/singletons/save/CompanyAssessment

```

```auto
{"error":"Call to a member function isResourceEditableByCurrentUser() on
null","file":"\/var\/www\/html\/modules\/Singletons\/Controller\/Admin.php","line":157}

```

Any ideas why it isn’t working?

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [March 30, 2022, 1:48pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/11 "2022-03-30T13:48:30Z")

</div>

My simple solution with revoking the method from the Admin controller doesn’t work anymore since March 2019, when the resource lock was implemented.

> <https://github.com/agentejo/cockpit/commit/917ba5afdcb9e8504fa5fbedc0e4b76563a6c62e#diff-817f20061f136b73d4e6c31c78802c243e4e07d43eef31208355f3512a6a69c9>

You have to adapt the code from `modules/Singletons/Controller/Admin.php` → `update_data()` in your custom endpoint to avoid the user status check.

> <https://github.com/agentejo/cockpit/blob/next/modules/Singletons/Controller/Admin.php#L142-L176>

---

<div class="post-metadata">

**Author:** ![seank1968](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/seank1968/32/1002_2.png) [@seank1968](https://discourse.getcockpit.com/u/seank1968)\
**Post date:** [February 9, 2023, 1:56pm UTC](https://discourse.getcockpit.com/t/update-collection-and-singleton-via-api/404/12 "2023-02-09T13:56:12Z")

</div>

Also trying to to this - what needs to be done to get this to work?

Thanks
