React, Next.js and Laravel
Add the chat to a React, Next.js or Laravel app, control it from your code, and call the API from your server.
Everything here uses the same two pieces as every website: the script tag (the chat window) and the
REST API (your server). Your assistant's public id (ast_pub_…) is on its Install tab. The
chat only appears on the domains in its Websites tab — add localhost there while you develop.
React
Load the widget once, for example in your root component:
import { useEffect } from 'react';
export function Chat({ assistantId }: { assistantId: string }) {
useEffect(() => {
const script = document.createElement('script');
script.src = 'https://antalkchat.com/widget/v1.js';
script.dataset.assistant = assistantId;
script.async = true;
document.body.append(script);
}, [assistantId]);
return null;
}
Render <Chat assistantId="ast_pub_YOUR_ID" /> once per page. The chat stays when the visitor moves between your
routes.
Next.js
In the App Router, add the script to app/layout.tsx:
import Script from 'next/script';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://antalkchat.com/widget/v1.js"
data-assistant="ast_pub_YOUR_ID"
strategy="afterInteractive"
/>
</body>
</html>
);
}
In the Pages Router, put the same <Script> in pages/_app.tsx.
Control the chat from your code
Once the widget has loaded, window.Assistant opens it, asks a question or tells you what happened:
window.Assistant.open();
window.Assistant.send('How much is delivery to Chattogram?');
window.Assistant.on('message', (answer) =>
console.log(answer.status, answer.text),
);
Events: ready, open, close, message (an answer finished) and error (for example origin_not_allowed).
close(), toggle() and off(event, listener) work too.
Laravel
Keep the id and your API key in config/services.php, read from .env:
'antalkchat' => [
'url' => env('ANTALKCHAT_URL', 'https://antalkchat.com'),
'assistant' => env('ANTALKCHAT_ASSISTANT'),
'key' => env('ANTALKCHAT_KEY'), // sk_live_… — server only
'webhook_secret' => env('ANTALKCHAT_WEBHOOK_SECRET'),
],
Show the chat from your Blade layout, before </body>:
<script src="{{ config('services.antalkchat.url') }}/widget/v1.js"
data-assistant="{{ config('services.antalkchat.assistant') }}" async></script>
Ask the assistant from your server, for example from a support form:
use Illuminate\Support\Facades\Http;
$reply = Http::withToken(config('services.antalkchat.key'))
->acceptJson()
->timeout(30)
->post(config('services.antalkchat.url').'/v1/assistants/'.config('services.antalkchat.assistant').'/chat', [
'message' => $request->input('question'),
])
->throw()
->json();
$answer = $reply['answer']; // the text
$conversation = $reply['conversation_id']; // send it back to continue the chat
Receive webhooks in a route outside the web middleware group (no CSRF token), and check the
signature before you trust the body:
use Illuminate\Http\Request;
Route::post('/antalkchat/webhook', function (Request $request) {
parse_str(str_replace(',', '&', (string) $request->header('X-AnTalkChat-Signature')), $parts);
$time = (int) ($parts['t'] ?? 0);
$expected = hash_hmac('sha256', $time.'.'.$request->getContent(), config('services.antalkchat.webhook_secret'));
abort_unless(abs(time() - $time) < 300 && hash_equals($expected, (string) ($parts['v1'] ?? '')), 403);
$event = $request->json()->all(); // {id, type, created_at, data}
// Use $event['id'] (or the X-AnTalkChat-Delivery header) to ignore an event you already handled.
return response()->noContent();
});
Security
- The assistant id is public; your API key (
sk_live_…) and webhook secret are not. Keep them in.envon your server, never in JavaScript, a mobile app or a Git repository. - A Content-Security-Policy needs
script-srcandconnect-srcforhttps://antalkchat.com.