# Using api from custom component

**URL:** https://meta.discourse.org/t/using-api-from-custom-component/304641
**Category:** Development
**Tags:** plugin-api
**Created:** [April 19, 2024, 4:39pm UTC](https://meta.discourse.org/t/using-api-from-custom-component/304641 "2024-04-19T16:39:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gar1t](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gar1t/32/380142_2.png) [@gar1t](https://meta.discourse.org/u/gar1t)
#### Post date: [April 19, 2024, 4:39pm UTC](https://meta.discourse.org/t/using-api-from-custom-component/304641/1 "2024-04-19T16:39:33Z")

</div>

I’m trying to access the api from a custom component with this snipped in the html body:

```plaintext
<script type="text/discourse-plugin" version="0.8">
  console.log("###########", api.getCurrentUser());
</script>

```

This snippet however does not appear in the page HTML.

What’s the correct approach here?

Btw, this is the same question posed a few years ago but not answered:

> [@How to insert custom html through jquery using plugin-api events](https://meta.discourse.org/t/how-to-insert-custom-html-through-jquery-using-plugin-api-events/180330):
>
> I have tried to add custom html through jquery using plugin api events like api.onAppEvent(), api.decorateWidget(), etc…, in Edit CSS/HTML \>\> /head section. But, the events which i tried is not triggering the jquery on page load. So, i am little bit confused about what event need to use. Because that event should trigger the jquery throughout the site on page load instead of blocking the scripts on particular pages. Here is the code which i tried,

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [April 19, 2024, 4:52pm UTC](https://meta.discourse.org/t/using-api-from-custom-component/304641/2 "2024-04-19T16:52:07Z")

</div>

Hello Garrett!

If you put this code in your theme to theme component `Head` tab,

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/0/b/e0bdbee83fd5c10da9d1983c6bb183d39459fa52.png)

You should be able to see the output in the browser’s _console_ (there is no HTML being displayed here):

![image](https://global.discourse-cdn.com/meta/original/4X/f/4/a/f4aba1939dccc54de802aff0463e029206c26c78.png)

Useful guide to learn about Theme component (also #Customization > Theme component tag) 👍

> [@Developing Discourse Themes & Theme Components](https://meta.discourse.org/t/beginners-guide-to-developing-discourse-themes/93648):
>
> Discourse Themes and Theme Components can be used to customize the look, feel and functionality of Discourse’s frontend. This section of the developer guides aims to provide all the reference materials you need to develop simple themes for a single site, right up to complex open-source theme components. This introduction aims to provide a map of all the tools and APIs for theme development. If you prefer a step-by-step tutorial for theme development, jump straight to: Themes vs. Theme Compon…

---

<div class="post-metadata">

### Author: ![gar1t](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gar1t/32/380142_2.png) [@gar1t](https://meta.discourse.org/u/gar1t)
#### Post date: [April 19, 2024, 4:58pm UTC](https://meta.discourse.org/t/using-api-from-custom-component/304641/3 "2024-04-19T16:58:27Z")

</div>

Thank you. I had placed this code in the body section.

It’s surprising to me that the head behavior here is different. It looks like Discourse is removing these api snippets from the body section. Other script elements are retained there.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [May 19, 2024, 4:59pm UTC](https://meta.discourse.org/t/using-api-from-custom-component/304641/4 "2024-05-19T16:59:17Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
