# How can I make an AJAX or API call in an addon

**URL:** <https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360>\
**Category:** Addons\
**Created:** [May 12, 2022, 8:22am UTC](https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360 "2022-05-12T08:22:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolf](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rolf](https://discourse.getcockpit.com/u/Rolf)\
**Post date:** [May 12, 2022, 8:22am UTC](https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360/1 "2022-05-12T08:22:41Z")

</div>

How can I make an AJAX or API call in an addon, i.e. via javascript (cockpit frontend), e.g. if I have a chart and generate the data in the backend and prepare it as JSON?

---

<div class="post-metadata">

**Author:** ![Jamo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/jamo/32/1117_2.png) [@Jamo](https://discourse.getcockpit.com/u/Jamo)\
**Post date:** [May 12, 2022, 11:55am UTC](https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360/2 "2022-05-12T11:55:41Z")

</div>

Hi @Rolf

You want to get data from Cockpit in your frontend, right?  
If that’s the case, please refer to [the docs](https://getcockpit.com/documentation/api).

Let me know if this helped.

---

<div class="post-metadata">

**Author:** ![Rolf](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rolf](https://discourse.getcockpit.com/u/Rolf)\
**Post date:** [May 12, 2022, 12:07pm UTC](https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360/3 "2022-05-12T12:07:42Z")

</div>

not in my frontend. in the fronted of cockpit.  
the ajax-call must call on a part of the code from addon. and the fontend part of same plugin must call over javascript on this same area of plugin.

---

<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:** [July 10, 2022, 9:27am UTC](https://discourse.getcockpit.com/t/how-can-i-make-an-ajax-or-api-call-in-an-addon/2360/4 "2022-07-10T09:27:39Z")

</div>

@Rolf Have a look at the core modules and `App.request()`.

In short:

```php
$app->bind('/mydata', function() {
    return ['key' => 'value'];
});

```

```nohighlight
App.request('/mydata').then(function(data) {
    console.log(data);
});

```

References:

> <https://github.com/agentejo/cockpit/blob/next/modules/Collections/views/partials/entries.php#L509>

> <https://github.com/agentejo/cockpit/blob/next/modules/Collections/Controller/Admin.php#L452>

> <https://github.com/agentejo/cockpit/blob/next/assets/app/js/app.js#L25>
