- Install the React SDK
- Set up the necessary providers
- Connect to Towns
- Create a space
- Send your first message
Installation
You can start a new Towns project in two ways:Using create-towns-protocol-app (Recommended)
The easiest way to get started is usingcreate-towns-protocol-app and follow the instructions in the terminal. This will set up a new React project with all the necessary dependencies and configurations.
Manual Installation
If you prefer to add the React SDK to an existing project, install the required dependencies:vite-plugin-node-polyfills to your vite.config.ts:
vite.config.ts
Setting Up Providers
Towns requires a few providers to be set up at the root of your application:WagmiProvider- For Web3 wallet connection (recommended)TownsSyncProvider- For interacting with Towns Protocol
App.tsx
Connecting to Towns Protocol
You can connect to Towns using either a Web3 wallet (recommended) or a bearer token. You can use the following networks:omega- The Towns mainnet, uses Base as the EVM chaingamma- The Towns testnet, uses Base Sepolia as the EVM chain
gamma as a starting point.
You can use other gamma clients like Towns (gamma) or the Towns Playground to help you inspect messages on the
gamma network.Using a Web3 Wallet
You’ll need to use
getEthersSigner to get the signer from viem wallet client.
Towns SDK uses ethers under the hood, so you’ll need to convert the viem wallet client to an ethers signer.You can get the getEthersSigner function from Wagmi docs.Using a Bearer Token
You can connect to Towns using a pre-existing identity. This allows you to read and send messages, but you won’t be able to create spaces or channels (on-chain actions).If you have a Towns account, you can get your bearer token from there. Type
/bearer-token in any conversation to get your token.Creating a Space
Once connected, you can start interacting with Towns. Here’s how to create a space:You can only create a space with a Web3 wallet. If you’re using a bearer token, you can’t create spaces.
Sending Your First Message
With the space created, we can send a message to the#general channel.
Here’s how to create a form
ChatApp component to your app and you’re ready to start chatting!
Next Steps
Now that you have the basics set up, you can:- Create and join spaces
- Create channels
- Send messages
- Read messages
- Send reactions
- Set username
- Read message threads
- much more! You can find the full list of hooks in the API Reference.