# Cant inject javascript into Post API (solved)

**URL:** <https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239>\
**Category:** Integrations & API\
**Created:** [April 1, 2024, 7:25pm UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239 "2024-04-01T19:25:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 1, 2024, 7:25pm UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239/1 "2024-04-01T19:25:46Z")

</div>

hello  
i cant inject javascript into body, header or footer using post API html.  
how to do that?  
wrap in `<div>` stil not working  
not wraping with anything it was parsed as `<p>`

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [April 1, 2024, 8:19pm UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239/2 "2024-04-01T20:19:23Z")

</div>

There are two ways you could approach it:

1. Using post HTML  
You’ll be able to add a script tag to the body HTML using the API, but you’ll want to ensure that you’re wrapping your HTML like this:

```auto
<!--kg-card-begin: html-->
<p>HTML goes here</p>
<!--kg-card-end: html-->

```

> **[Ghost Admin API Documentation](https://ghost.org/docs/admin-api/#the-post-object)**
>
> Manage content via Ghost's Admin API, with secure role-based authentication. Read more on Ghost Docs 👉

1. Use Code Injection via the API  
Alternatively, you can write a script tag to `codeinjection_foot` or `codeinjection_head` via the API call for the post.

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 1, 2024, 9:03pm UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239/3 "2024-04-01T21:03:42Z")

</div>

thank you very much for the reply.

`<div><script>console.log('hello')</script></div>`  
failed

`<pre><script>console.log('hello')</script></pre>`  
failed

will try using  
`<!--kg-card-begin: html-->`

method 2, failed.  
post the script like  
…

`"codeinjection_foot": <script>console.log('hello')</script>,`

…  
cant find it when checking the post thru admin page.

or is it because of cloudflare. but i already bypass WAF for admin api

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [April 1, 2024, 9:21pm UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239/4 "2024-04-01T21:21:43Z")

</div>

I tested the API call with this property for Code Injection:

```auto
codeinjection_foot: `<script>console.log('Hello from the foot')</script>`

```

And like this in the post body:

```auto
html: "<!--kg-card-begin: html--><p>Some post content</p><script>console.log('Hello from the foot')</script><!--kg-card-end: html-->",

```

I can confirm that both methods worked. It could be something happening with Cloudflare or something in how you’re coding your API calls. We’ll need some more info to help!

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 8:21am UTC](https://forum.ghost.org/t/cant-inject-javascript-into-post-api-solved/46239/5 "2024-04-02T08:21:49Z")

</div>

> [@RyanF](#):
>
> `codeinjection_foot: `console.log(‘Hello from the foot’)\</scri`

i can confirm both method works perfectly.  
thank you very much RyanF 🙏
