Install

Install the Polaris JavaScript SDK using npm.

Terminal
npm install @getpolaris.ai/sdk

Configure Laravel with Vite

If you're using Vite in your Laravel project, you can configure Polaris using the following steps.

Create Environment Variable

Open the .env file in the root of your Laravel project and add the following line.

ESM
VITE_POLARIS_API_KEY="API_KEY"

Be sure to replace API_KEY with your application's API key.

Create polaris.js

Create a new JavaScript file named polaris.js in the public/js folder of your Laravel project to configure Polaris using your application's API key.

ESM
import { createConnection } from '@getpolaris.ai/sdk';

createConnection({ apiKey: import.meta.env.VITE_POLARIS_API_KEY }).catch(console.error);

Import polaris in app.js

Open the app.js file in the resources/js folder and import the polaris module.

ESM
import './polaris';

Load app.js in app.blade.php

Be sure to load the app.js file using the @vite directive to your app.blade.php file.

PHP
@vite('resources/js/app.js')

Configure Laravel with Mix

If you're using Mix + webpack in your Laravel project, you can configure Polaris using the following steps.

Create Environment Variable

Open the .env file in the root of your Laravel project and add the following line.

ESM
MIX_POLARIS_API_KEY="API_KEY"

Be sure to replace API_KEY with your application's API key.

Create polaris.js

Create a new JavaScript file named polaris.js in the public/js folder of your Laravel project to configure Polaris using your application's API key.

ESM
import { createConnection } from '@getpolaris.ai/sdk';

createConnection({ apiKey: process.env.MIX_POLARIS_API_KEY }).catch(console.error);

Be sure to replace API_KEY with your application's API key.

Import polaris in app.js

Open the app.js file in the resources/js folder and import the polaris module.

ESM
import './polaris';

Load app.js in app.blade.php

Open the app.blade.php file in the resources/views/layouts folder and load the app.js file using the mix directive.

HTML
<script src="{{ mix('js/app.js') }}" defer></script>

Verify Installation

Now, verify that the connection was successful by checking the console. If there is an error connecting to Polaris you will see an error message in the console. If there is not an error, then you're good to go! 🎉