# API - Username instead of id on \_by/\_mby

**URL:** <https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063>\
**Category:** Support\
**Created:** [September 4, 2021, 6:38pm UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063 "2021-09-04T18:38:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![linhous](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/linhous/32/900_2.png) [@linhous](https://discourse.getcockpit.com/u/linhous)\
**Post date:** [September 4, 2021, 6:38pm UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/1 "2021-09-04T18:38:58Z")

</div>

Hi Guys/Girls

First of all I wanted to make it very clear that, I love Cockpit, I just intended to test it and now I’m using it on everything hahaha.

But I have a question, when requesting /collections/get/posts for example, I get the field \_by and \_mby which in this case, is referring to the ID of the user who created/changed the entry, right?

How do I bring the username instead of the ID? Because, when adding the AccountLink in the entry, it also returns the ID instead of the Name or even the object with some data of the user who created the entry.

Can anyone give me a hand?  
Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![ronaldaug](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/ronaldaug/32/260_2.png) [@ronaldaug](https://discourse.getcockpit.com/u/ronaldaug)\
**Post date:** [September 5, 2021, 5:27am UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/2 "2021-09-05T05:27:48Z")

</div>

> [@linhous](#):
>
> How do I bring the username instead of the ID? Because, when adding the AccountLink in the entry, it also returns the ID instead of the Name or even the object with some data of the user who created the entry.

By default, it only show id as you said, you have to add `populate=1` to display all fields from account link.  
e.g

```auto
/collections/get/posts?token=1234567890&populate=1

```

## Edit Note

> `populate` only work with `CollectionLink`, not `Account-Link`.

---

<div class="post-metadata">

**Author:** ![linhous](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/linhous/32/900_2.png) [@linhous](https://discourse.getcockpit.com/u/linhous)\
**Post date:** [September 5, 2021, 6:05am UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/3 "2021-09-05T06:05:16Z")

</div>

I understood,

In a way, it helped me a lot to know about this “populate=1”, I confess that I didn’t know.  
I needed to show the Name of a collection called Category, linked as CollectionLink, but the display was just the Slug, and with this “Populate” it worked to show all fields 🙂

But with AccountLink, even using “Populate=1”, it doesn’t show the username or any other field, it just returns the ID anyway ☹

---

<div class="post-metadata">

**Author:** ![ronaldaug](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/ronaldaug/32/260_2.png) [@ronaldaug](https://discourse.getcockpit.com/u/ronaldaug)\
**Post date:** [September 5, 2021, 8:05am UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/4 "2021-09-05T08:05:56Z")

</div>

> [@linhous](#):
>
> But with AccountLink, even using “Populate=1”, it doesn’t show the username or any other field, it just returns the ID anyway ☹

My bad, I should have mentioned that `populate` only work with `collectionLink`.

And you can’t retrieve accountLink data with collection data at the same time. Maybe it could be the security reason.

### Option 1

Fire one more API request with account ID to get the user data

### Option 2

Ref answer by raffaelj: [How to refer to another Collections or "Accounts" - #7 by raffaelj](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/7)

- create `config` directory in the root folder, under that directory create `bootstrap.php` file. Add the below codes. Make sure you don’t expose account sensitive data.

> Replace “collection\_name” with your actual collection name.

```auto
<?php

// This will append "author" object into API response. 

if (COCKPIT_API_REQUEST) {

    $app->on('collections.find.after.collection_name', function($name, &$entries) {

        function getSomeFields($array){
            $author_name = !empty($array['name'])?$array['name']:$array['user'];

            // Only return 2 fields "name" and "_id"
            return [
            "name" => $author_name,
            "_id" => $array['_id']
           ];
        }

        foreach ($entries as &$entry) {

                $account = $this->storage->findOne('cockpit/accounts', ['_id' => $entry['_by']]);
                $account = !empty($account)?getSomeFields($account):[];

                // You can rename "author" as you want                
                $entry['author'] = $account;

        }

    });

}

```

---

<div class="post-metadata">

**Author:** ![linhous](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/linhous/32/900_2.png) [@linhous](https://discourse.getcockpit.com/u/linhous)\
**Post date:** [September 5, 2021, 2:32pm UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/5 "2021-09-05T14:32:40Z")

</div>

> [@ronaldaug](#):
>
> ### Option 1
> 
> Fire one more API request with account ID to get the user data

This I really thought about doing, but I thought it could be less performant, even more so with a relevant amount of data to consult…

> [@ronaldaug](#):
>
> ### Option 2
> 
> Ref answer by raffaelj: [How to refer to another Collections or “Accounts” - #7 by raffaelj](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/7)
> 
> - create `config` directory in the root folder, under that directory create `bootstrap.php` file. Add the below codes. Make sure you don’t expose account sensitive data.
> 
> > Replace “collection\_name” with your actual collection name.

My friend, this is awesome, it worked perfectly for me, and it opened my mind to do other things, it may be my fault, but is there anything in the documentation talking about creating this “bootstrap.php” file? I didn’t think… Anyway, thank you very much, it worked perfectly!

---

<div class="post-metadata">

**Author:** ![ronaldaug](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/ronaldaug/32/260_2.png) [@ronaldaug](https://discourse.getcockpit.com/u/ronaldaug)\
**Post date:** [September 6, 2021, 6:53am UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/6 "2021-09-06T06:53:49Z")

</div>

> [@linhous](#):
>
> is there anything in the documentation talking about creating this “bootstrap.php” file?

I don’t think the official docs have the detail about building `addons` and doing some logic on `hook events`.  
I can share you how I understand it.

> Disclaimer - the below information is based on my experience, so I could be wrong

`config/bootstrap.php` is like an init file. But it’s used for general purpose and you can add your common codes inside it.

For a specific type of init file, you can create your own addon under addons directory.  
So, it would be something like this - `addons/{your_addon_name}/bootstrap.php`

* * *

Let me explain how a very simple `addon` works.

```php
if (COCKPIT_API_REQUEST) {
    $app->on('cockpit.rest.init', function ($routes) use ($app) {
        $routes['test'] = function () use ($app) {
             $my_variable = $this->param('my_variable'); 
        }
     } 
}

```

### API Authentication

`COCKPIT_API_REQUEST` is API authentication / middleware

```php
if(COCKPIT_API_REQUEST){
    // The requests must includes token to access this 
}

```

* * *

### Event

`cockpit.reset.init` is like your codes will execute when Cockpit CMS init.  
This is very similar to `Jquery` or `JS` events.

```auto
 $app->on('cockpit.rest.init', function ($routes) use ($app) {
         // do your logic here
     } 

```

So, you might say how do I know those events?  
Here is the list of Cockpit events.

> <https://github.com/raffaelj/cockpit-scripts/blob/master/cli/get-triggers_list-of-events.md>

* * *

### Routes

To declare routes, you can write as below.

```php
if (COCKPIT_API_REQUEST) {
    $app->on('cockpit.rest.init', function ($routes) use ($app) {
        $routes['test'] = function () use ($app) {
             // access api route at "api/test" and do your logic here 
       }
    })
}

```

This is an example `test` route, so you can access your route like `/api/test`

* * *

### Request Parameters

You can simply grab the request parameter like `$this->param("my_variable")`

e.g

```php
     $my_variable = $this->param('my_variable'); 

```

* * *

### Lime framework

Cockpit was built on top of `Lime` micro php framework [https://github.com/agentejo/lime](https://github.com/agentejo/lime)  
So, most of it basic functions would be the same as lime.

You can also learn from this scripts list by @raffaelj

> **[GitHub - raffaelj/cockpit-scripts: A collection of code snippets with annotations to...](https://github.com/raffaelj/cockpit-scripts)**
>
> A collection of code snippets with annotations to modify Cockpit CMS

Hope this also help someone.

---

<div class="post-metadata">

**Author:** ![linhous](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/linhous/32/900_2.png) [@linhous](https://discourse.getcockpit.com/u/linhous)\
**Post date:** [September 6, 2021, 6:51pm UTC](https://discourse.getcockpit.com/t/api-username-instead-of-id-on-by-mby/2063/7 "2021-09-06T18:51:20Z")

</div>

That was very enlightening, I’ll save it all here!  
It will definitely be of great help, thank you very much!
