> For the complete documentation index, see [llms.txt](https://docs.themeupstudio.com/crimson/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.themeupstudio.com/crimson/getting-started/code-injection.md).

# Code Injection

### Code Injection (Site Header)

<code class="expression">space.vars.THEME\_NAME</code> reads a few **global JavaScript variables** (Content API key + behavior flags) **before the theme scripts run**, so this snippet must go into **Ghost → Code Injection → Site Header** (the `<head>` area).

#### Step 1 — Open Code Injection in Ghost Admin

1. Log in to your Ghost Admin.
2. Go to **Settings → Advanced → Code Injection**.

<figure><img src="/files/qAhwR8ttPWVX4rfRhBEi" alt=""><figcaption><p>Code injection module in Ghost Admin Setting</p></figcaption></figure>

#### Step 2 — Paste the snippet into “Site Header”

1. Find the **Site Header** box (not Site Footer). Paste your snippet exactly as-is into **Site Header**.
2. Replace the example values with your own (details below).
3. Click **Save**.

{% hint style="danger" %}
Ensure you are adding the script in **Site header**&#x20;
{% endhint %}

<figure><img src="/files/KIXQmhzfoEq5nAdVioUW" alt=""><figcaption><p>Paste the script in Site header and click Save</p></figcaption></figure>

Here’s a **copy/paste-ready** version with placeholders:

{% code title="Code injection in Site header of your Ghost website. Copy and Paste this script as is then update the variable values as per your preferances." lineNumbers="true" %}

```html
<script>
  window.GHOST_CONTENT_API_KEY = '1234a5b6cde7fg8901h234i4j5';
  // optional if your site uses a different canonical domain:
  // window.GHOST_API_URL = 'https://your-site.tld';
</script>
<script>
    // Override GLOBAL variables before theme loads them
    window.GLOBAL = {
        //1. Open external links in a new tab?
        //accepted values: true, false
        //default: true
        OPEN_LINKS_IN_NEW_TAB: true,
        
        //2. Enable image lightbox popup?
        //accepted values: true, false
        //default: true
        ENABLE_IMAGE_LIGHTBOX: true,
        
        //3. Auto hide the navigation header when scrolling down?
        //accepted values: true, false
        //default: true
        AUTO_HIDE_HEADER: true,
        
        //4. Default color scheme (light/dark/system)
        //accepted values: 'dark', 'light', 'system'
        //default: 'system'
        DEFAULT_COLOR_SCHEME: 'system',
        
        //5. Turn on if you want to automatically redirect a visitor to the website in visitor's browser language.
        //accepted values: true, false
        //default: false
        REDIRECT_ON_LOCALE: false,
        
        //6. Pagination style (load-more/paginated-pages) - Only applicable on paginated content for posts. Authors and Tags pages will always be 'load more' button.
        //accepted values: 'load-more', 'paginated-pages'
        //default: 'load-more'
        PAGINATION_STYLE: 'load-more',

        //7. Contact Endpoint for Contact form on Contact page
        //If no endpoint is added, the contact form will be hidden
        //default: '' - form will be hidden
        CONTACT_ENDPOINT: 'https://example.com/api/contact',
      
        //8. Contact Page WhatsApp link
        //If no link is added, the WhatsApp will be hidden
        WHATSAPP_LINK: '',
      
        //9. Use locale-specific domains instead of /{lang}/ paths? Provide locale-specific domains in LOCALE_DOMAINS variable below.
        USE_LOCALE_DOMAINS: false,//accepted values: true, false
      
        //10. Map language code to domain (e.g., { en: "englishdomain.com", hi: "hindidomain.com", es: "spanishdomain.com"})
        //E.g: LOCALE_DOMAINS: {
        //          en: 'www.example.com',
        //          hi: 'hi.example.com',
        //          es: 'es.example.com'
        //        }
        LOCALE_DOMAINS: {},
      
        //11. When switching locale domains, open the homepage instead of keeping the current path?
        LOCALE_DOMAIN_HOME_ONLY: false,//accepted values: true, false
      
        //12. Comma-separated paths to keep when LOCALE_DOMAIN_HOME_ONLY is enabled (e.g., "account, membership, signin")
        LOCALE_DOMAIN_HOME_IGNORE_PATHS: '',//empty means redirect all paths to the homepage
    };
</script>
```

{% endcode %}

{% hint style="info" %}
Site social links are no longer configured in Code Injection, except for WHATSAPP\_LINK. Crimson now reads site social accounts from Ghost Admin >  Settings > General settings > Social accounts.
{% endhint %}

**Why Site Header (not Footer)?**

Ghost places Site Header code inside `<head>`, while Site Footer code is injected before `</body>`. For <code class="expression">space.vars.THEME\_NAME</code>, the goal is to define `window.GLOBAL` **before** the theme JS reads it.

#### Step 3 — Create a Custom Integration (to get the Content API key)

Ghost’s official docs recommend generating your **Content API URL + key** by creating a **Custom Integration**.

1. In Ghost Admin, go to **Settings → Integrations**.
2. Click **Add custom integration**.
3. Name it something like: <code class="expression">space.vars.THEME\_NAME</code> Theme.
4. Click **Add.**
5. After creation, a popup will appear. Copy these from the popup:
   * **Content API Key** → to be pasted into `window.GHOST_CONTENT_API_KEY`
   * **API URL** (Optional)(or Content API URL details shown there) → use only if you need `window.GHOST_API_URL`
6. Click **Save** in the Custom integration popup.
7. Open Code Injection module ([explained earlier](#step-1-open-code-injection-in-ghost-admin)).
8. Paste the Content API key value for the variable `window.GHOST_CONTENT_API_KEY`
9. (Optional) Paste the API URL value for the variable `window.GHOST_API_URL`
10. Click **Save** button.

{% hint style="success" %}
**Content API** keys are **safe to use in the browser** because they only allow **read-only access to public content**.
{% endhint %}

{% hint style="warning" %}
Your **Admin API key** is different and must never be placed in client-side code. (<code class="expression">space.vars.THEME\_NAME</code> doesn’t need Admin API for normal use.)
{% endhint %}

Below are the step-by-step snapshots for adding the Content API key (and API URL) in Code injection, so <code class="expression">space.vars.THEME\_NAME</code> can use it.

<figure><img src="/files/Cdjy8hIRvr8wLWrgzH5c" alt=""><figcaption><p>Navigate to Integrations module in Ghost Admin Setting and Click Add custom integration</p></figcaption></figure>

<figure><img src="/files/Q3LPlrpfTnqI4e6yfga8" alt=""><figcaption><p>Add the integration's name and click Add</p></figcaption></figure>

<figure><img src="/files/ah3PStl4eEukjRL1X4Pb" alt=""><figcaption><p>Copy the Content API key (API URL is optional) and click Save</p></figcaption></figure>

<figure><img src="/files/mIVtQgOdRRimYPfrpN6e" alt=""><figcaption><p>Paste the copied Content API Key in the Code injection script and click Save</p></figcaption></figure>

#### Step 4 — When to set `window.GHOST_API_URL`

Leave it **commented** unless one of these applies:

* You are on **Ghost(Pro)** and your **API domain** differs from your public site domain.
* You are using a **staging/canonical** setup where the theme runs on one domain but the API must be called on another.
* You are doing a headless/front-end scenario.

{% hint style="info" %}
If you are unsure, add the API URL from custom integration to the `window.Ghost_API_URL` variable in Code injection.
{% endhint %}

In the next section, we will look at the variables defined in Code injection and their purpose.
