Code Examples
Ready-to-use implementation examples for common AI Anywhere use cases.
These code examples demonstrate real-world implementations of AI Anywhere. Copy and adapt them for your own projects.
Latest HTML Integration
Modern HTML5 implementation with semantic markup. Add the script to your <head> section and use data attributes for configuration.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Contact form with AI assistance">
<title>Contact Form</title>
<!-- AI Anywhere Helper Script -->
<script src="https://ai-anywhere.ai/sdk/v1/loader.js"
data-site="your-site-id"
defer></script>
</head>
<body>
<main>
<form id="contact-form">
<label for="message">Your Message:</label>
<textarea id="message"
name="message"
rows="5"
placeholder="Type your message..."
required></textarea>
<span data-helper-anchor
data-helper-token="your-helper-token"></span>
<button type="submit">Send</button>
</form>
</main>
</body>
</html>
Contact Form Example
A complete contact form with Helper assistance for the message field.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Us</title>
<script src="https://ai-anywhere.ai/sdk/v1/loader.js" data-site="your-site-id"></script>
</head>
<body>
<form id="contact-form">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="6" required></textarea>
<span data-helper-anchor data-helper-token="your-helper-token"></span>
</div>
<button type="submit">Send Message</button>
</form>
</body>
</html>
Multiple Text Areas with Different Helpers
Initialize multiple Helpers with different configurations for different form fields.
<script src="https://ai-anywhere.ai/sdk/v1/loader.js" data-site="your-site-id"></script>
<form>
<label>Product Description:</label>
<textarea id="description"></textarea>
<span data-helper-anchor data-helper-token="product-helper-token"></span>
<label>Internal Notes:</label>
<textarea id="notes"></textarea>
<span data-helper-anchor data-helper-token="notes-helper-token"></span>
</form>
WordPress Integration
Add AI Anywhere to your WordPress theme by including the snippet in your theme's header.php or using a plugin.
<?php
// Add to your theme's functions.php or a custom plugin
function add_ai_anywhere_helper() {
?>
<script src="https://ai-anywhere.ai/sdk/v1/loader.js" data-site="<?php echo get_option('aia_site_id', 'your-site-id'); ?>"></script>
<span data-helper-anchor data-helper-token="<?php echo get_option('aia_helper_token', 'your-helper-token'); ?>"></span>
<?php
}
add_action('wp_footer', 'add_ai_anywhere_helper');
?>
Store your Helper configuration in WordPress options for easy management.
React Integration
Integrate AI Anywhere into your React application using hooks and TypeScript support. Works with React 16.8+ and modern React patterns.
import { useEffect, useRef } from 'react';
interface ContactFormProps {
siteId: string;
helperToken: string;
}
function ContactForm({ siteId, helperToken }: ContactFormProps) {
const scriptLoaded = useRef(false);
useEffect(() => {
// Load AI Anywhere script only once
if (!scriptLoaded.current) {
const script = document.createElement('script');
script.src = 'https://ai-anywhere.ai/sdk/v1/loader.js';
script.setAttribute('data-site', siteId);
script.defer = true;
document.head.appendChild(script);
scriptLoaded.current = true;
return () => {
// Cleanup on unmount
const existingScript = document.querySelector(
'script[src="https://ai-anywhere.ai/sdk/v1/loader.js"]'
);
if (existingScript) {
document.head.removeChild(existingScript);
}
scriptLoaded.current = false;
};
}
}, [siteId]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
// Your form submission logic
};
return (
<form onSubmit={handleSubmit}>
<label htmlFor="message">Your Message:</label>
<textarea
id="message"
name="message"
placeholder="Type your message..."
required
/>
<span
data-helper-anchor
data-helper-token={helperToken}
></span>
<button type="submit">Send</button>
</form>
);
}
export default ContactForm;
Usage: Import and use the component: <ContactForm siteId="your-site-id" helperToken="your-helper-token" />
Angular CLI Integration
Add AI Anywhere to your Angular application using Angular services and lifecycle hooks. Compatible with Angular 12+.
// contact-form.component.ts
import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-contact-form',
template: `
<form (ngSubmit)="onSubmit()">
<label for="message">Your Message:</label>
<textarea
id="message"
name="message"
[(ngModel)]="message"
placeholder="Type your message..."
required></textarea>
<span
data-helper-anchor
[attr.data-helper-token]="helperToken"></span>
<button type="submit">Send</button>
</form>
`,
styleUrls: ['./contact-form.component.css']
})
export class ContactFormComponent implements OnInit, OnDestroy {
message = '';
siteId = 'your-site-id';
helperToken = 'your-helper-token';
private scriptElement?: HTMLScriptElement;
constructor(@Inject(DOCUMENT) private document: Document) {}
ngOnInit(): void {
// Load AI Anywhere script
this.scriptElement = this.document.createElement('script');
this.scriptElement.src = 'https://ai-anywhere.ai/sdk/v1/loader.js';
this.scriptElement.setAttribute('data-site', this.siteId);
this.scriptElement.defer = true;
this.document.head.appendChild(this.scriptElement);
}
ngOnDestroy(): void {
// Cleanup script on component destroy
if (this.scriptElement && this.document.head.contains(this.scriptElement)) {
this.document.head.removeChild(this.scriptElement);
}
}
onSubmit(): void {
// Your form submission logic
console.log('Message:', this.message);
}
}
Note: Make sure to import FormsModule in your app.module.ts for ngModel to work.
Vue.js Integration
Integrate AI Anywhere into your Vue.js application using composition API or options API. Works with Vue 3.
<template>
<form @submit.prevent="handleSubmit">
<label for="message">Your Message:</label>
<textarea
id="message"
v-model="message"
placeholder="Type your message..."
required></textarea>
<span
data-helper-anchor
:data-helper-token="helperToken"></span>
<button type="submit">Send</button>
</form>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
const message = ref('');
const siteId = 'your-site-id';
const helperToken = 'your-helper-token';
let scriptElement: HTMLScriptElement | null = null;
onMounted(() => {
// Load AI Anywhere script
scriptElement = document.createElement('script');
scriptElement.src = 'https://ai-anywhere.ai/sdk/v1/loader.js';
scriptElement.setAttribute('data-site', siteId);
scriptElement.defer = true;
document.head.appendChild(scriptElement);
});
onUnmounted(() => {
// Cleanup script on component unmount
if (scriptElement && document.head.contains(scriptElement)) {
document.head.removeChild(scriptElement);
}
});
const handleSubmit = () => {
// Your form submission logic
console.log('Message:', message.value);
};
</script>
Alternative (Options API): Use mounted() and beforeUnmount() hooks if you prefer the Options API.
Vanilla SPA Integration
Add AI Anywhere to a vanilla JavaScript Single Page Application. Works with any routing library or framework-free SPA architecture.
// ai-anywhere-loader.js
class AIAnywhereLoader {
constructor(siteId) {
this.siteId = siteId;
this.scriptLoaded = false;
}
load() {
if (this.scriptLoaded) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
// Check if script already exists
const existingScript = document.querySelector(
'script[src="https://ai-anywhere.ai/sdk/v1/loader.js"]'
);
if (existingScript) {
this.scriptLoaded = true;
resolve();
return;
}
// Create and load script
const script = document.createElement('script');
script.src = 'https://ai-anywhere.ai/sdk/v1/loader.js';
script.setAttribute('data-site', this.siteId);
script.defer = true;
script.onload = () => {
this.scriptLoaded = true;
resolve();
};
script.onerror = () => {
reject(new Error('Failed to load AI Anywhere script'));
};
document.head.appendChild(script);
});
}
unload() {
const script = document.querySelector(
'script[src="https://ai-anywhere.ai/sdk/v1/loader.js"]'
);
if (script) {
document.head.removeChild(script);
this.scriptLoaded = false;
}
}
}
// Initialize on app load
const aiLoader = new AIAnywhereLoader('your-site-id');
// Load when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => aiLoader.load());
} else {
aiLoader.load();
}
// Usage in your SPA routes
function renderContactForm() {
return `
<form id="contact-form">
<label for="message">Your Message:</label>
<textarea id="message" placeholder="Type your message..." required></textarea>
<span data-helper-anchor data-helper-token="your-helper-token"></span>
<button type="submit">Send</button>
</form>
`;
}
Usage: Import the loader in your main app file and use it across your SPA routes.
Custom Styling Example
Override default Helper button styles to match your site's design.
<style>
/* Customize Helper button appearance */
.ai-helper-button {
background-color: #007bff !important;
border-radius: 8px !important;
padding: 0.5rem 1rem !important;
font-size: 0.875rem !important;
}
.ai-helper-button:hover {
background-color: #0056b3 !important;
}
</style>
<script src="https://ai-anywhere.ai/sdk/v1/loader.js" data-site="your-site-id"></script>
<form>
<textarea id="message" placeholder="Your message"></textarea>
<span data-helper-anchor data-helper-token="your-helper-token"></span>
</form>