Build an app with sign-in and AI #
On this page
Prompt:
Build a web app where people sign in and chat with an AI that can follow the conversation. I don't have an AI provider key.
This example uses the complete server-ai-chat source. The browser signs people in and keeps the conversation while the page stays open, and the backend sends recent turns to the model.
Reloading the page clears the conversation. The server keeps no conversation history. For private notes saved in a database, use Build a notes app with sign-in and a database.
A shared database is one monthly item for the application; stored files are charged by size (Stay inside the Free Allowance).
At a glance:
- Example:
server-ai-chat, with all its files on one source page. - Sign-in:
/__account/signin; the Account package verifies the session insrc/session.mjs. - AI route:
POST /api/ask;src/ai.mjscalls the AI Generation package after the server verifies the user. - Conversation: held in browser memory; the model receives at most the latest eleven turns, each at most 4,000 characters.
- Packages: Account and AI Generation. The manifest declares the accounts service, with no database.
What your AI does #
Your AI assistant starts from the complete example, creates the application, submits its manifest, and deploys it. It checks that anonymous requests cannot reach the AI route and that a signed-in conversation sends earlier turns with a follow-up. It reports which checks it completed.
What you need #
- A connected AI coding agent.
- Room under the account's limit of 10 applications. AI calls through the platform draw the account's balance.
- A sign-in method to try, such as an emailed code, Google, or GitHub.
You need no separate AI provider account or key.
Before your AI starts #
For the AI assistant: Read the skill:deploy, skill:add-sign-in, and skill:use-ai-generation skills through read_context, and keep their approval and verification rules. On the command route, read the files step 2's scaffold writes; you need not also read the example's source page. On the file route, read the complete server-ai-chat example, and follow each continuation to read every file.
For an app the person keeps, use the turnzero command from its first deploy where it works. Use the command also for binary files, a second deploy, scaffolding, or local runs. With a shell, try the npx form first; no global installation is needed. The command needs Node.js 24 or later and an authorized computer. If the command cannot run, use the file route within its limits; a first simple test app the person will discard may also use files when none of those command uses applies.
Before choosing the file route, tell the person that deploy_files alone provides no local tests or local run. If you have no shell, or your shell is elsewhere, this route writes no project files on the person's computer.
With a shell and Node.js but no usable npx, follow Local tests without npx before the first deploy. Missing npx alone does not prevent local tests.
Steps #
1. Create the application #
Read read_plan_quotas for the sizes' limits. Use the size the person named, or ask them to choose. With no size named and nobody to ask, use Shared 0.5 GB with its default sleep setting. Report the size, that nobody was available, its awake-time charge and the option to request Shared 1 GB for more memory or CPU. Call create_application with that size and keep its identifier. If the account is full, follow Create the application. A new application has production only.
2. Take the complete example #
On the command route, if the person will run the commands, give them the command checklist before their first command. Before scaffolding, authorize that computer.
Use an empty project folder on the person's computer. If your shell is elsewhere, the person runs the scaffold in that folder in their own terminal. Give them the exact line below with <application id> replaced by step 1's identifier. On Windows, use npx.cmd in place of npx; it works in any Windows shell.
npx --no-update-notifier -y turnzero@next scaffold --origin https://dev.turnzero.ai --template server-ai-chat --application <application id>
The command writes the app in app/, takes the Account and AI Generation packages, installs dependencies, and runs the tests. Keep the generated files together.
On the file route, read every file on the server-ai-chat source page. Use the paths in the file table and leave off the download URLs' .txt suffix. Keep package.json, the client files, and server modules. Read the tests too, even if your tools cannot run them.
For the file route, call read_library with no entry to read the inventory. Put the current names and versions of account and ai_generation in the manifest's packages list. Keep the example's file:lib/… dependencies. Leave the two library folders out of deploy_files; Cloud fills them from the submitted manifest. The manifest's package rows explains this route.
3. Follow a conversation from sign-in to the model #
The complete app connects these parts:
| File | What happens |
|---|---|
client/index.html |
The Sign in link opens the platform's sign-in page on the app's own hostname. |
client/app.js |
The page reads /__account/me and keeps the conversation in memory. Each new message posts the history as messages to /api/ask, with the browser's session cookie. |
src/session.mjs |
The Account client verifies the cookie or bearer token and the router's session header. It returns the verified user's id, or no user. |
src/server.mjs |
The route refuses an unverified request with 401 sign_in_required. A verified request passes its history to the AI code. |
src/ai.mjs |
The AI code calls generateText with the recent turns under the app's credential and returns the model's answer or a fallback that names the failure. |
Each item in messages has role and content. The history must alternate between user and assistant, starting and ending with user. A malformed history, a blank turn, or a turn over 4,000 characters is refused with 400 invalid_messages. A body that is not valid JSON is refused with 400 invalid_json.
The server sends at most the latest eleven turns to the model, starting with a user turn, and keeps no history after the request. Earlier turns remain in the page but are outside that model call. Reloading the page clears the conversation held by the page.
If the route refuses or returns no answer text, the page removes that unanswered user turn from the history it sends next. The next question therefore follows the last completed exchange.
Keep the platform credential in the backend. The browser receives the answer, never that credential. AI calls on the platform's key draw the account's balance at the provider's published price plus a markup. Read the current charge on the pricing page, or call read_rate_card. You need no stored provider key or external-host declaration for this route.
4. Submit the manifest and deploy #
Submit the example's complete manifest with submit_manifest, naming no local_run. It declares the accounts service alone. Use the application identifier from step 1. For the file route, include the current package rows prepared in step 2; the source template's empty list is a starting point.
For a local run, follow the scaffold's printed steps after this submission. Run its provision line from app/ with nothing piped. If it prints a submit_manifest call, make that call and run the line it returns as given from app/.
Provision writes the local run's settings into .env; it is not needed to deploy. Once the settings are written, follow the scaffold's steps for running and shaping the app locally before the deployment check below.
Call read_app_sign_in for the environment you plan to test and keep the sign-in methods the person chose. It reads production by default. Pass environment: "development" when you plan to test in development. Until configured, the end-user realm offers Google, GitHub, emailed codes, and passkeys with open sign-up. The realm is the environment's user directory. An invited audience makes sign-up invitation-only.
Call check_deploy with the application identifier. Resolve its refusals and repeat the check after fixing what it reports. An empty list means the check found no configuration refusals. Test the app's code separately.
- With the command, run the check's returned
command_npxfromapp/, orcommand_npx_windowson Windows. Once the check passes, calldeploy_commandwith the application identifier alone and run that response's command fromapp/. Use its npx form first, or the installed form if npx cannot run andturnzero --versionworks. - With files, use
check_deploy'swithout_commandcall withcheck_only: trueand step 2's complete file list, at application-relative paths. Leave outnode_modules, credentials, and the two library folders Cloud fills. Once the result isfiles_checked, calldeploy_fileswithoutcheck_only, listing every file by its returnedpathandsha256. Ifmissingnames a hash, send that file's content again.
Inline files are limited to 512 KiB each; a stored entry avoids that per-file cap. List at most 200 files. Their combined size, including the library files Cloud fills, must not exceed 4 MiB. Each JSON request also has a 4 MiB limit. Beyond those limits, follow Ways to deploy; never truncate a file to fit.
Follow the returned read_status call with wait_seconds until deployment ends. If a response leaves the outcome uncertain, read status before retrying. On deployed, record the version and hostname, then verify the app in step 5. For a refusal, follow its detail and deployment refusals.
With a shell, run the app's tests again after any code change. Without one, report that local tests were not run. File checks and a successful health check do not verify sign-in or an AI answer.
5. Verify sign-in and a follow-up #
Check the deployed home page and /health. Send an anonymous GET /api/ask; it must return 401 with sign_in_required. The example checks sign-in before the method or body, so this GET verifies the anonymous refusal without calling the AI.
Use a temporary test end user to check the conversation at the deployed address. Ask, “Name three ways to organize a small project.” Then ask, “Explain the second one.” Check that the follow-up uses the earlier exchange. Reload the page and confirm that its conversation clears. Sign out and confirm that the signed-in controls disappear.
Follow Test sign-in with test end users to create and sign in the test user. For an automated run, check a signed-in POST /api/ask as well as the anonymous refusal. Send messages in the form step 3 describes, including the earlier user turn and answer before the follow-up.
When verifying the first deploy on production, use your AI assistant's browser-signed-in connection or a token with the test_end_users grant to create a temporary test end user on production. When testing in development, create the temporary test end user in development. Use that user for signed-in reads or the app's AI chat in the same environment.
These checks need no further confirmation, even if the app stores that user's own chat turns. Removing the data these checks wrote for that test user also needs no further confirmation. The test user expires after 48 hours. On an app with real users, ask the owner to confirm any other production write; a request to build, deploy, or test alone does not approve it.
On an app with no real users, test writes on development where available, otherwise freely on its live copy. Report the test rows you wrote so the person can remove them before launch. Email beyond the person's own test addresses and paid calls with stored keys still need their request for this run. Check the release defines when an app has no real users.
Follow the test-user guide's cleanup steps. The example keeps chat turns only in the page's memory. If your app saves turns, use its own route to remove the turns this check wrote for the test user. If the app has no cleanup route, leave the rows in place and tell the person which rows remain and where. The application's saved data remains when the test user expires.
After the test-user checks and cleanup, have the person sign in at the deployed address and check that the signed-in controls appear.
Use server-ai-chat sign-in and conversation as the example name in your verification report. Record the environment, whether browser sign-in completed, the anonymous refusal, and whether the follow-up used the earlier exchange. Record whether reloading cleared the conversation, and state any untested part explicitly.
Name each local test or deployed check you ran, its outcome, and whether it ran before or after deployment. For each check left unrun, give the reason. Keep a test result separate from a successful deploy, health check or page load.
If your host refuses a check, respect that refusal and report its method, path and environment as untested, with the reason. Local tests or a request using another method do not establish that the refused check ran.
Expected result #
The deployed app lets a person sign in and have an AI conversation, with AI calls charged to the account's balance. The browser keeps the turns until the page reloads; the server sends recent turns without storing them. Anonymous requests cannot use the AI route. The verification report distinguishes local tests from checks on the deployed app.
Refusals #
These are the example's common failures. The Account, AI Generation, and platform refusal reference cover the rest.
| Code | Cause | Remedy |
|---|---|---|
sign_in_required |
The app received no verified session. | Sign in on the deployed app's hostname, or follow the test-user guide for an automated request. This response is expected in the anonymous check. |
library_entry_not_served |
Cloud cannot fill a declared library copy at its recorded version. | Send the project's own copy. If the entry is served and the person chooses that version, update its manifest row, submit the manifest, and retry with the root file: dependency kept and the copy omitted. |
invalid_messages |
The history is malformed, a turn is blank, or a turn exceeds 4,000 characters. | Send non-blank { role, content } turns, alternating between user and assistant, starting and ending with user, within the per-turn limit. |
invalid_json |
The request body is not valid JSON. | Send a JSON object with a messages array. |
account_paused |
If the gateway refuses an AI call from work that was already running when the account paused, the example app returns 503. The router returns 429 for new requests during the pause. | Follow Handle an account pause. Do not retry the call. |
internal |
An AI refusal has no named fallback in the example. The route answers 500. | Read the server logs for the cause and report the failure. |
allowance_unavailable |
The platform cannot serve the AI call on its key. | Keep the fallback and report the failure if it persists. |
upstream_quota |
The model provider is out of capacity. | Keep the fallback and try later. |
Related #
- server-ai-chat example holds the complete app and its tests.
- Build a notes app with sign-in and a database builds the private-notes example.
- Add sign-in to your app explains session verification and the sign-in methods.
- Call an AI model explains the AI request, conversation history, and fallbacks.
- Test your application locally explains tests over the package doubles.