Skip to content

Start with TanStack Start

Learn how to setup your first TanStack Start project powered by Appwrite.

1

Head to the Appwrite Console.

Create project screen

Create project screen

If this is your first time using Appwrite, create an account and create your first project.

Then, under Add a platform, add a Web app. The Hostname should be localhost.

Cross-Origin Resource Sharing (CORS)

Adding localhost as a platform lets your local app talk to Appwrite. For production, add your live domain to avoid CORS errors.

Learn more in our CORS error guide.

Add a platform

Add a platform

You can skip optional steps.

2

Create a TanStack Start project.

Shell
npm create @tanstack/start@latest my-app && cd my-app
3

Install the JavaScript Appwrite SDK.

Shell
npm install appwrite
4

Find your project's ID in the Settings page.

Project settings screen

Project settings screen

Create a new file src/utils/appwrite.ts and add the following code to it, replace <PROJECT_ID> with your project ID.

Web
import { Client, Account, ID, Models } from 'appwrite';

export const client = new Client();

client
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>'); // Replace with your project ID

export const account = new Account(client);
export { ID };
export type { Models };
5

Create or update src/routes/index.tsx with the following code.

React
import { useState } from 'react';
import { createFileRoute } from '@tanstack/react-router';
import { account, ID, type Models } from '../utils/appwrite';

export const Route = createFileRoute('/')({
    component: Index,
});

function Index() {
    const [loggedInUser, setLoggedInUser] = useState<Models.User<Models.Preferences> | null>(null);
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [name, setName] = useState('');

    async function login(email: string, password: string) {
        await account.createEmailPasswordSession({
            email,
            password,
        });
        setLoggedInUser(await account.get());
    }

    async function register() {
        await account.create({
            userId: ID.unique(),
            email,
            password,
            name,
        });
        await login(email, password);
    }

    async function logout() {
        await account.deleteSession({ sessionId: 'current' });
        setLoggedInUser(null);
    }

    if (loggedInUser) {
        return (
            <div>
                <p>Logged in as {loggedInUser.name}</p>
                <button type="button" onClick={logout}>
                    Logout
                </button>
            </div>
        );
    }

    return (
        <div>
            <p>Not logged in</p>
            <form>
                <input
                    type="email"
                    placeholder="Email"
                    value={email}
                    onChange={(e) => setEmail(e.target.value)}
                />
                <input
                    type="password"
                    placeholder="Password"
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                />
                <input
                    type="text"
                    placeholder="Name"
                    value={name}
                    onChange={(e) => setName(e.target.value)}
                />
                <button type="button" onClick={() => login(email, password)}>
                    Login
                </button>
                <button type="button" onClick={register}>
                    Register
                </button>
            </form>
        </div>
    );
}
6

Run your project with npm run dev and open localhost on port 3000 in your browser.