Adding an AI chatbot to an app used to mean weeks of work: a language model, a backend, a chat interface, and a lot of glue code. Today you can have a working chatbot in minutes, and you can even let AI write most of the interface for you. This guide pulls together four practical approaches: calling the Claude API from Python, getting AI to generate a complete chat interface, adding free AI to a web app with Puter.js, and building a native iPhone app by talking to Claude inside Xcode. By the end you’ll know which route fits your project and how to avoid the biggest security mistake beginners make.
Four ways to add a chatbot to your app
There is no single “right” way. The best route depends on whether you want to learn the API, generate an interface quickly, avoid paying for usage, or build a native iOS app.
| Approach | What you get | Coding needed | Who pays for AI usage |
|---|---|---|---|
| Claude API with Python | A working chatbot logic you fully control | A little Python | You (pay per use) |
| AI-generated chat interface | A modern HTML, CSS and JavaScript chat UI with dark mode and typing animation | None, just copy and paste | Depends on what you connect it to |
| Puter.js | AI chat, image generation and text to speech in a web app with one script tag | Basic JavaScript | Each user, using their own Puter account |
| Claude in Xcode | A native iPhone app written in Swift and SwiftUI | None, you describe features in plain language | Your Claude subscription (for building the app) |
The one rule: never ship your API key inside the app
Before any code, understand the problem that trips up almost every first-time builder. A traditional AI API needs a secret key, and there are no free credits to start with, so you pay upfront. Worse, every person using your app burns your credits. If your app goes viral, your bill goes with it.
And if you put that key directly into app code, anyone can extract it. That is a security nightmare.
The safe pattern for a real mobile app: the app talks to your own small backend, and only the backend holds the API key and calls the AI provider. Your app never sees the key. The sections below show the API itself, a no-backend option (Puter.js) where users cover their own usage, and a native iOS workflow, so you can pick what fits.
Creating an Anthropic API key
You need a key before the Python code will work.
- Search for Anthropic API in your browser, open the official link and click Start building.
- Sign up or log in.
- Go to Settings and open API Keys.
- Click Create new API key, choose a workspace and give the key a name.
- Copy the key and store it somewhere safe. You will not be able to view it again.
A useful tip for managing several keys: create separate workspaces from the Workspaces section so each project’s keys stay organized.
Building a chatbot with the Claude API in Python
Install the official package:
pip install anthropic
(Use pip3 if that is how Python is set up on your machine.) The chatbot is an infinite loop: read what the user types, stop if they type exit, otherwise send the message to Claude and print the reply.
import os
import anthropic
# Read the key from an environment variable instead of pasting it into the code
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
while True:
user_message = input("You: ")
if user_message.lower() == "exit":
break
response = client.messages.create(
model="claude-sonnet-5-5", # use a model that is currently available
max_tokens=1000, # limit on the length of the reply
messages=[
{"role": "user", "content": user_message}
],
)
print("Claude:", response.content[0].text)
Run it, say hello and it greets you back. Ask about black holes and you get an elaborate answer. But there is a catch.
Making the chatbot remember the conversation
Ask the chatbot to “summarize what you just said” and it has no idea what you mean. The API does not remember anything between requests. Every call is independent, so you must send the whole conversation each time.
The fix is a list that stores every message. Add the user’s message to the list before the request, then add the chatbot’s reply after it.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
conversation = [] # holds the full chat history
while True:
user_message = input("You: ")
if user_message.lower() == "exit":
break
conversation.append({"role": "user", "content": user_message})
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1000,
messages=conversation, # send the whole history, not just the last message
)
reply = response.content[0].text
conversation.append({"role": "assistant", "content": reply})
print("Claude:", reply)
Now ask about black holes, then ask for a summary, and it summarizes its own previous answer correctly.
Keep your key out of the code. The walkthrough pastes the key straight into the script, which is fine for a quick test, but it’s safer to set it as an environment variable (as in the code above) so it never ends up in files you share or upload to GitHub.
Letting AI build the chat interface
A terminal chatbot works, but users expect a real interface. You can have Claude generate a complete, modern chat interface in about ten minutes with no coding knowledge. The secret is a detailed prompt: the better the prompt, the better the output. It’s like giving a chef a detailed recipe versus just saying “make me food.”
Step 1: Write a detailed prompt
Structure your prompt with clear headings and bullet points covering a project overview, the file structure and specific requirements for each file. Here is a sample prompt in that style:
Create a modern AI chatbot interface with three files:
index.html, style.css and script.js.
HTML requirements:
- Clean structure with proper meta tags
CSS requirements:
- Modern, minimalist design
- Responsive layout that works on phone and laptop
- Good color schemes for both light and dark themes
JavaScript requirements:
- Message sending system
- Automatic replies from the bot
- Typing indicator animation (three bouncing dots)
- Theme switching that remembers the user's choice
Step 2: Generate and copy the code
Log in to Claude (sign up takes about 30 seconds; a Google account works), paste the whole prompt into the chat box and let it generate. You should get three separate files: index.html for structure, style.css for styling and script.js for functionality. Copy each one completely, from the first line to the last.
Step 3: Set up VS Code
- Open VS Code (it’s free) and create three files named
index.html,style.cssandscript.js. - Paste the matching code into each file and save all (
Ctrl+SorCmd+S). - If you don’t have it yet, open Extensions, search for Live Server and install it.
- Right-click
index.htmland choose Open with Live Server. Your chatbot opens in the browser at a localhost address.
Step 4: Test everything
- Type “hello” and press Enter. You should see a smooth animation and bouncing dots while the bot “thinks”.
- Click the sun icon to switch to dark mode, then click again to return to light mode. Refresh to confirm it remembers your choice.
- Resize the window or open it on a phone to check the responsive layout.
Important: A prompt like this produces the interface with simulated bot replies. To make the bot answer with a real AI model, you connect the interface to an API through a backend (see the mobile section) or use a service like Puter.js (next section).
Free AI in a web app with Puter.js
Puter.js is a JavaScript library that lets you add AI chat, image generation and text to speech to a web app with a single script tag. There is no signup with OpenAI, no API key to manage and no backend or server. It has a large following on GitHub and gives access to a range of popular text, image and speech models.
Setup
If you use Vue, React or another framework, install it with npm and import it. For plain HTML, add the script tag in your page:
<script src="https://js.puter.com/v2/"></script>
Text chat
The function you need for any chat app is puter.ai.chat. You pass the text and the model name.
<script>
puter.ai.chat("Write a three-line poem about coffee", {
model: "MODEL_NAME_FROM_PUTER_DOCS"
}).then(function (response) {
document.getElementById("output").textContent = response;
});
</script>
Model names change often, so copy a currently supported one from Puter’s documentation. In the demo, the chat app had a dropdown with a handful of models for the user to choose from. To run the page locally you need a server, which is why the Live Server extension from the previous section is handy: press Go Live at the bottom of VS Code.
Image generation
For images, the function is puter.ai.txt2img. You give it a prompt and pick one of the supported image models.
puter.ai.txt2img("A futuristic city at sunset", {
model: "IMAGE_MODEL_FROM_PUTER_DOCS"
}).then(function (image) {
document.body.appendChild(image); // shows the generated image
});
In the demo app, clicking a suggested prompt generated the image, with buttons to create a new one or download the result. Text to speech works the same way with its own supported models.
How can it be free? The user-pays model
This is the important part. Puter calls it the user-pays model:
- A user opens your app and tries to chat or generate an image.
- A pop-up asks them to sign in with, or create, a Puter account.
- They use their own free allowance first.
- When that runs out, they are prompted to add credits to their own account.
You are never charged, even if a million people use your app, because every user uses their own account.
| Traditional API | Puter.js (user-pays) | |
|---|---|---|
| Who pays | You pay for all users’ usage | Each user pays for their own |
| API keys | You must manage and secure them | None needed |
| Backend | Usually required | Not required |
| Cost as you grow | Grows with users | No cost to you at any scale |
| Abuse and rate limits | Your problem | Not on you, since each user has their own account |
Know the trade-off. “Free” is free for the developer. Your users must create a Puter account and may eventually need to add credits. That’s fine for many tools and experiments, but not ideal if you want a seamless, signup-free experience for every visitor, or full control over which model and provider you use.
Building a native iPhone app with Claude in Xcode
If you want a real iPhone app rather than a web page, you can work with Claude directly inside Xcode. Claude in Xcode is a coding assistant integration from Apple and Anthropic that became generally available in September 2025. It runs in Xcode’s intelligence panel, understands the context of your project, answers questions in plain language, remembers what was discussed during the session, and can debug, refactor and generate new features. You describe what you want in plain language and it writes the Swift and SwiftUI code.
What you need
- A Mac with Apple Silicon running macOS 26 (Tahoe) or later, as required by Xcode 26.
- Xcode 26 from the Mac App Store. It’s free, but plan for roughly 10 GB of free space for the app and simulators.
- A Claude Pro or Max account (an Anthropic API key is also supported if you prefer more control over usage and billing).
- An iPhone running iOS 26 or the built-in simulator. A physical device is helpful but not required.
- No Swift experience.
Connect Claude to Xcode
- Install Xcode 26 from the Mac App Store, open it and accept the license agreement.
- Open Xcode Settings and go to the Intelligence section.
- Select Anthropic and sign in with your Claude account.
- Return to the intelligence panel and confirm that Claude shows as the active model.
Create the project
From the welcome screen choose Create new project, pick the iOS App template, name it (the demo uses “Journal App”) and make sure SwiftUI is selected as the interface. You don’t need to understand every file Xcode generates. From here, your job is to describe each feature clearly, let Claude update the project and test the result.
The key idea: five small prompts, not one giant one
The demo built a journal app (create, edit and delete entries with a mood) by splitting the work into five focused conversations. Each step was tested before moving on, and Claude kept everything from earlier steps intact.
| Step | Prompt (summarized) | What to check |
|---|---|---|
| 1. Data model | Create a journal entry model with a title, text body, date created and a mood from a small set of options. Use SwiftData so entries are saved locally and are still there the next time the app opens. | The generated files include title, body, date and mood |
| 2. Main list | Build the main screen as a scrollable list of all entries, most recent first. Each row shows the title, date and a small mood icon. | App runs in the simulator; the list is empty for now |
| 3. New entry screen | Build a screen with a title field, a text area for the body and a row of emoji options for mood. Add a Save button that adds the entry to the list and returns to the main screen. | Create an entry; it appears in the list immediately |
| 4. Detail, edit, delete | Open a detail screen when an entry is tapped, showing the full title, body, date and mood. Add an Edit button to change any field and save, and a Delete button that removes the entry and returns to the list. | Create, read, edit and delete all work together |
| 5. Native design | Review the full app and apply clean native iOS design: standard navigation bars, appropriate fonts and spacing, a cohesive color scheme, standard list and form patterns, a dark and light theme and smooth animations. | All three screens look consistent in both themes |
Test on a real iPhone
- Connect your iPhone to the Mac with a USB cable.
- In Xcode’s device selector, choose your iPhone instead of the simulator.
- Run the project. Xcode builds the app, installs it and launches it.
- Open the app from the home screen and repeat the full flow: create, view, edit and save.
This is a genuine native iOS app, not a website in a mobile wrapper, so it can use native iPhone features and keep growing like any other Xcode project.
Want a chatbot in this app? The same five-step method applies. Create the data model for messages, build the chat list, add the input screen, polish the design, and only then add a prompt that connects the screen to your backend. Test after every step.
Connecting a chatbot to a real mobile app
Putting the pieces together, a production-ready setup looks like this:
- The interface lives in your app (Flutter, React Native, SwiftUI or a web page). It shows messages and sends the user’s text to your server.
- Your small backend holds the API key, receives the message, adds it to the stored conversation history and calls the Claude API, exactly like the Python example.
- The reply goes back to the app, which displays it (ideally with a typing indicator while waiting).
Remember that the API doesn’t remember anything, so your backend must store each user’s conversation and send the history with every request. If you don’t want to run a backend and are building for the web, Puter.js is the shortcut, with users covering their own usage.
Common mistakes and gotchas
| Symptom | Likely cause |
|---|---|
| Chatbot forgets what you said a moment ago | You’re sending only the latest message. Send the whole conversation list every time. |
| Your API bill jumps unexpectedly | Keys exposed in app code, or no limits. Keep the key on a backend and set sensible max_tokens values. |
| You lost your API key | Keys are shown only once. Create a new key and keep it in a safe place. |
| Live Server doesn’t open the page | The extension isn’t installed, or you’re not right-clicking the correct index.html. Install Live Server from Extensions. |
| Generated interface has fake replies only | That is expected from a UI-only prompt. Connect it to a real API through a backend. |
| Puter.js asks users to sign in | That’s how the user-pays model works. Each user needs their own Puter account. |
| Puter code stops working after a while | Model names change. Check Puter’s documentation for currently supported models. |
| Xcode can’t connect Claude | Check that you chose Anthropic in Settings, then Intelligence, signed in with a Pro or Max account and that Claude shows as active. |
| Output breaks after a big AI-generated change | Prompt too large. Split the work into small prompts and test between each one. |
Frequently asked questions
Do I need coding knowledge to add an AI chatbot to my app?
Not necessarily. AI tools can generate the interface and even a complete native iOS app from plain-language prompts. For connecting to a real AI model securely you’ll need a little code, but the Python example above is short and easy to follow.
Is it safe to put my API key in my mobile app?
No. Anyone can extract a key from app code. Keep it on a backend you control and have your app call that backend.
Why does my chatbot forget the conversation?
AI APIs don’t store history between requests. Keep a list of all messages and send the entire list each time.
Is Puter.js really free?
It is free for you as the developer. Each user signs in with their own Puter account and uses their own free allowance first, then adds credits if they want to continue.
Should I use Puter.js or the Claude API directly?
Use Puter.js for quick web projects where you don’t want to manage keys or a server. Use the API directly with your own backend when you want full control over the model, behavior, costs and user experience.
Do I need a paid Claude plan to use Claude in Xcode?
Yes. You need a Claude Pro or Max plan, or alternatively an Anthropic API key.
Is an app built with Claude in Xcode a real app?
Yes. Claude writes real Swift and SwiftUI code that runs natively on iOS, and you can continue developing it like any other Xcode project.
Can I reuse this workflow for other app ideas?
Yes. A recipe box, reading list, habit tracker or notes app follows the same structure: data model, list, creation screen, detail view, then design.
Conclusion
Adding an AI chatbot to an app is no longer a big project. Start with the Claude API in Python to understand how requests and conversation memory work, use a detailed prompt to generate a polished interface, try Puter.js when you want AI in a web app without managing keys, and use Claude in Xcode when you want a native iPhone app built one small prompt at a time.
Whichever route you choose, remember the three rules that matter most: write specific prompts, send the full conversation history with every request, and never put your secret API key inside an app. Pick one simple idea, build it step by step, test after every change and you’ll have a working chatbot faster than you expect.