AI explained

How to embed ChatGPT on your website safely

A chat box on your site is the easy part. Keeping keys private, limiting scope and handing off to people is the real work. See the safe pattern and an example embed.

By the Askocto team · · 4 min read

To embed ChatGPT on your website, you place a chat interface on your pages and connect it to a language model through a server you control. The chat box is the easy part. Protecting your API key, limiting what the assistant can say and giving customers a way to reach a person are what separate a safe embed from an expensive mistake. This article covers the pattern and its main pitfalls.

In short: Never call a model directly from the browser with your key. Route messages through a server or a platform, ground the answers in your own content and make handoff part of the design.

What "embed" can mean

People use the word for three different things, and the differences matter:

  1. An iframe or link to a provider's chat page. Simple, but the provider's interface, not yours, and not always permitted for this use. Check the provider's current documentation and terms.
  2. A custom chat box that calls a model API. You build the interface and a server in the middle.
  3. A hosted widget from a support platform. You paste a script, and the platform runs the conversation, the content and the model connection.

The rest of this article is mostly about the second and third, because they put the chat on your own page and let you shape what it does.

The safe pattern in four parts

1. Keep the API key on a server

Anything in your page code can be read by any visitor. If a model API key sits in your JavaScript, someone can copy it and run up your bill or misuse your account. Your chat box should send messages to your own server, and that server adds the key and calls the model.

2. Add your content

A model on its own answers from general knowledge. For customers, it must answer from your policies and product details, and say when the answer is not there. That means retrieval over your content, covered in RAG from scratch.

3. Limit scope and abuse

Decide what the assistant may discuss, and what it must refuse. Public chat boxes attract off-topic requests and attempts to override instructions. Add rules for scope, limit message length and request rates per visitor, and log what happens.

4. Provide a way out

A chat box with no handoff traps customers who need a person. Offer a route to a human, with the conversation summary passed along.

A pre-launch checklist

  • The API key is stored on the server and never appears in page source.
  • Requests are rate limited per visitor.
  • The assistant has a written scope and refusal list.
  • Answers come from approved content, and "I don't know" is an allowed answer.
  • Personal data is not requested unless needed, and is handled according to your policy. This is not legal advice.
  • A visible AI disclosure tells visitors they are talking to an AI.
  • A handoff path exists and was tested.
  • Conversations are logged and someone reviews a sample every week.
  • Errors show a friendly fallback, not a blank box.

Embedding with a platform

With a platform, the key handling, content retrieval and handoff are part of the product. In Askocto the steps are:

  1. Create an agent and add sources under Build > Sources.
  2. Test it under Test > Playground.
  3. Open Channels, enable the website widget and copy the embed script.
  4. Paste it before the closing body tag of your site, or into your site builder's custom code box.

The script is one line:

<script src="https://cdn.askocto.co/embed.js" data-agent-id="agt_123" defer></script>

After it loads, a global window.askocto is available for control from your own code. You can open the chat from a button, pass what you know about a logged-in customer, and listen for events:

document.querySelector("#help").addEventListener("click", () => {
  window.askocto.open()
})

window.askocto.identify({
  userId: "usr_1042",
  userHash: "<signed on your server>",
  name: "Dana",
  email: "dana@example.com"
})

window.askocto.on("handoff", () => {
  console.log("Conversation passed to the team")
})

The userHash is created on your server by signing the user id with the agent's identity secret, so visitors cannot pretend to be someone else. The identity verification docs show how.

Choosing between a custom build and a platform

Consideration Custom chat box on a model API Platform widget
Control of look and behavior Full Configurable within the product
Key handling and rate limits You build and maintain Handled by the platform
Content retrieval You build Add sources and train
Handoff and inbox You build Helpdesk included
Time to launch Weeks Hours to days

If a custom build tempts you, the ChatGPT API guide lists the full set of pieces. If you prefer the platform route, embedding an AI chat widget on any website covers site builders and frameworks, and the overview of adding custom GPTs compares the options. See the plans to find where to start with Askocto.

Frequently asked questions

Can I put my OpenAI API key in the website code?

No. Anyone can read page code, so a key there can be copied and misused. Keep it on a server and let the browser talk only to your server.

Do I need a developer to embed an AI chat?

For a custom build, yes. For a platform widget, you usually only paste a script, though a developer helps if you want to pass customer details or custom actions.

How do I stop the chat from answering off-topic questions?

Write a clear scope in its instructions, add a list of topics to refuse, and test with off-topic and adversarial prompts before launch.

Will an embedded chat slow down my site?

The script loads separately and can use the defer attribute so it does not block the page. Test your page speed with the widget installed and adjust where it loads if needed.

Train an agent on your own help center

Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.

Keep reading

All articles
AI explainedclaude api
Jan 12, 20254 min read

Claude API for support bots: a production-readiness list

A support bot that calls a model API fails in ordinary ways: slow replies, outages, leaked keys, bad inputs. A production-readiness list with a retry example to adapt.

Read article
AI explainedgemini 3.6 flash
Feb 15, 20264 min read

Gemini 3.6 Flash: test speed against answer quality

Fast models are tempting for chat and voice, but speed is only worth it if answers stay right. A protocol to measure latency and quality together on your own conversations.

Read article
AI explainedkimi k2 api
Feb 13, 20264 min read

Kimi K2 API for support: an integration checklist

Using a model API in support means planning for slow calls, errors and changes. A pre-flight checklist and a thin wrapper keep Kimi K2 or any provider swappable.

Read article