> Add Plandalf to Nuxt with a hosted script in nuxt.config.ts or with the Vue package in a Nuxt plugin.

Source: https://plandalf.com/docs/frameworks/nuxt

Frameworks

# Nuxt

Load the hosted script in your Nuxt config to open checkouts from template attributes. To use composables in your pages and components, register the [Vue package](#the-plandalf-vue-package) in a Nuxt plugin.

## Add the script

Add the script to every page's `<head>` config, placed at the end of `<body>`. Copy your script address from your checkout's **Share** screen (pick **Vue** or **HTML**).

```ts
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          src: "https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js",
          async: true,
          tagPosition: "bodyClose",
        },
      ],
    },
  },
});
```

Restart the dev server after changing `nuxt.config.ts`. To load it on one page only, call `useHead({ script: [...] })` with the same object in that page.

## Open a checkout from a button

Add the attribute to any element in a template:

```html
<template>
  <button data-plandalf-present="YOUR-CHECKOUT">Buy Now</button>
</template>
```

### From your own click handler

Click handlers run in the browser, so `window` is safe there. Queue the call with the stub, so it works before the script has loaded:

```html
<script setup lang="ts">
function upgrade() {
  const w = window as any;
  w.plandalf = w.plandalf || function () { (w.plandalf.q = w.plandalf.q || []).push(arguments); };
  w.plandalf("present", "YOUR-CHECKOUT");
}
</script>

<template>
  <button @click="upgrade">Upgrade</button>
</template>
```

Don't touch `window` at the top of `<script setup>`: that code also runs on the server. See [Open a checkout from code](https://plandalf.com/docs/developers/open-a-checkout).

## Show a checkout on the page

Render an empty element with `data-plandalf-mount`:

```html
<template>
  <div data-plandalf-mount="YOUR-CHECKOUT" style="min-height: 480px"></div>
</template>
```

## Show a deadline timer

Render an empty element with your deadline timer's slug. The script draws the live countdown inside it. To charge the deadline timer's current price, add `data-plandalf-apply-promo` with the same slug to your buy button.

```html
<div data-plandalf-promo="YOUR-PROMO"></div>
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy Now</button>
```

See [Countdowns](https://plandalf.com/docs/promos/countdowns).

## The @plandalf/vue package

With Vue 3.5 or later, you can use the Vue package in a Nuxt plugin.

```bash
npm install @plandalf/vue
```

Create `plugins/plandalf.ts` in your Nuxt source directory. Use the same plugin on the server and in the browser. You can leave off the `.client` suffix.

```ts
import { createPlandalf } from '@plandalf/vue';

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(createPlandalf({ apiBase: 'https://YOUR-ORG.plandalf.dev' }));
});
```

In a page or component, import `usePresent` from `@plandalf/vue` and take `const { present } = usePresent()` in `<script setup>`. Call `present('YOUR-CHECKOUT')` from your button's click handler. See the [Vue guide](https://plandalf.com/docs/frameworks/vue) for the other composables.

## Check it's working

1.  ### Run the app
    
    If you used the hosted script, type `typeof window.plandalf` in the browser console. Look for `"function"`.
    
2.  ### Click your button
    
    Plandalf should display your checkout over the page. Navigate to another page and back, and check the button still works.
    
3.  ### Make a test purchase
    
    Add `localhost` and your staging site in Plandalf under **Settings → Advanced → Test domains**. Checkouts there use Stripe test cards. See [Test mode](https://plandalf.com/docs/get-started/test-mode).
    

## If it isn't working

-   A `window is not defined` error means code touched `window` during server rendering. Move it into an event handler or `onMounted`.
-   If your app sends a Content Security Policy, allow `https://*.plandalf.dev` and `https://js.stripe.com` in `script-src`, `connect-src` and `frame-src`.
-   Add `"data-debug": "true"` to the script object and the console logs each step the script takes.

See [Troubleshooting](https://plandalf.com/docs/help/troubleshooting).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
