# Ip restriction for api key?

**URL:** <https://discourse.getcockpit.com/t/ip-restriction-for-api-key/1412>\
**Category:** Support\
**Created:** [April 30, 2020, 8:41pm UTC](https://discourse.getcockpit.com/t/ip-restriction-for-api-key/1412 "2020-04-30T20:41:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusufiga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/yusufiga/32/134_2.png) [@yusufiga](https://discourse.getcockpit.com/u/yusufiga)\
**Post date:** [April 30, 2020, 8:41pm UTC](https://discourse.getcockpit.com/t/ip-restriction-for-api-key/1412/1 "2020-04-30T20:41:02Z")

</div>

Hi, I would like to restrict the usage of an api key for a white listed ip addresses. What would be the best way to achieve this.

I could probably do something like this

```
$app->on('collections.save.before.yourcollectionname', function($name, &$entry, $isUpdate) {
    if($apikey == "MY_API_KEY") {
        $allowed = $app->module('collections')->findOne('linked_col_name', ['ip' => $_SERVER['REMOTE_ADDR']]);
        if (!$allowed) {
            return 401;
        }
    }
});

```

But I don’t know how to get the api key from request at this point.

So, how can I get the api key from request or if not possible is there another way to do this?

Thanks

UPDATE:  
Referencing this class [https://github.com/agentejo/cockpit/blob/fc4db8a038d865fdd4e4faac911d117b741f4c7f/modules/Cockpit/rest-api.php](https://github.com/agentejo/cockpit/blob/fc4db8a038d865fdd4e4faac911d117b741f4c7f/modules/Cockpit/rest-api.php)

I tried to modify `modules/Cockpit/bootsrap.php` which also didn’t work

```
// REST
if (COCKPIT_API_REQUEST) {

    // INIT REST API HANDLER
    include_once( __DIR__.'/rest-api.php');

    $this->on('cockpit.rest.init', function($routes) {
        $token = $this->param('token', $this->request->server['HTTP_COCKPIT_TOKEN'] ?? $this->helper('utils')->getBearerToken());
        if($token == "MY_API_KEY") {
            $allowed = $app->module('collections')->findOne('whitelist', ['ip' => $_SERVER['REMOTE_ADDR']]);
            if (!$allowed) {
                return 401;
            }
        }
        $routes['cockpit'] = 'Cockpit\\Controller\\RestApi';
    });
}
```

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [May 7, 2020, 7:26pm UTC](https://discourse.getcockpit.com/t/ip-restriction-for-api-key/1412/2 "2020-05-07T19:26:08Z")

</div>

create following file: `config/bootstrap.php` with the following content:

```auto
<?php

$app->on('cockpit.rest.init', function() {

    if (!in_array($this->request->getClientIp(), ['172.1.1.1', '172.1.1.2'])) {
        $this->stop(401); // Unauthorized
    }
});

```

---

<div class="post-metadata">

**Author:** ![yusufiga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/yusufiga/32/134_2.png) [@yusufiga](https://discourse.getcockpit.com/u/yusufiga)\
**Post date:** [May 7, 2020, 8:00pm UTC](https://discourse.getcockpit.com/t/ip-restriction-for-api-key/1412/3 "2020-05-07T20:00:27Z")

</div>

Thank you for the reply.
