This tutorial walks through how to build an application with the
@stellar/stellar-sdk. To build with the Wallet SDK, please follow the Build a Wallet tutorial. To build with smart contracts, navigate to the Smart Contracts section.Project Setup
Project Requirements
To build a basic Stellar application, you’ll need:- Application framework: we’re using SvelteKit opting for JavaScript with JSDoc typing. SvelteKit is quite flexible and could be used for a lot, but we are mainly using it for its routing capabilities to minimize this being a “SvelteKit tutorial”.
- Frontend framework: we’re using DaisyUI to simplify the use of Tailwind CSS.
- A way to interact with the Stellar network: we’re using the
@stellar/stellar-sdk, but you could use traditional fetch requests. The@stellar/stellar-sdkis also used for building transactions to submit to the Stellar network. - A way to interact with the user’s keypair: we’re using
@stellar/typescript-wallet-sdk-km, but you can opt to use an existing wallet.
While we are using the above components to construct our application, we have done our best to write this tutorial in such a way that dependency on any one of these things is minimized. Ideally, you should be able to use the JavaScript code we’ve written and plug it into any other framework you’d like with minimal effort.
- We’ve designed this app for desktop. For the most part, the app is responsive to various screen sizes, but we have chosen not to go out of our way to prioritize the mobile user experience.
- We have enabled the default DaisyUI “light” and “dark” themes, which should switch with the preferences of your device. There is no toggle switch enabled, though.
- This is written as a client-side application. No server-side actions actually take place. If you are building an application with a backend and frontend, you will need to consider carefully which information lives where, especially when a user’s secret key is involved.
- We’re deploying this as a static “single-page application” with Cloudflare Pages. Your own deployment decisions will have an impact on your configuration and build process.
- The application is likely not as performant as it could be. Neither is it as optimized as it could be. We’ve tried to encapsulate the various functionalities in a way that makes sense to the developer reading the codebase, so there is some code duplication and things could be done in a “better” way.
- We do some error handling, but not nearly as much as you would want for a real-world application. If something seems like it’s not working, and you’re not seeing an error, open your developer console, and you might be able to figure out what has gone wrong.
- We have not implemented any automated testing. You’ll probably want some for your application.
This tutorial is probably best viewed as “nearly comprehensive.” We aren’t going to walk you through each and every file in our codebase, and the files we do use to illustrate concepts in the tutorial may not be entirely present or explained. However, we will cover the basics, and point you to more complete examples in the codebase when applicable.
Dev Helpers
- Stellar Lab: an experimental playground to interact with the Stellar network
- Friendbot: a bot that funds accounts with 10,000 fake XLM on Stellar’s Testnet; you can fund your account by going to
https://friendbot.stellar.org/?addr=G... - Testnet toml file: an example
stellar.tomlfile that demonstrates what information an anchor might publish - BasicPay dev helpers: if you’re using the BasicPay application, we’ve created a few helpful tools to help you explore its functionality
Getting Started
Here are the steps we’ve taken to start building BasicPay. Feel free to be inspired and customize these directions as you see fit. The entire BasicPay codebase is freely open and available on GitHub for reference.This part of the tutorial will need a large helping of “your mileage may vary.” We will outline what steps we’ve taken for our deployment situation, but you will want to review what options are needed for your environment(s).
Install Frameworks
The first thing we’ll need to do is create a SvelteKit app, usingnpm, we are using v18.x of nodejs.
- Which Svelte app template? Skeleton project
- Are you type checking with TypeScript? Yes, using JavaScript with JSDoc comments
- Select additional options Add ESLint for code linting; Add Prettier for code formatting
my-basic-payment-app directory. You can cd into that directory and add some UI dependencies.
tailwind.config.js and postcss.config.js files.
svelte.config.js file:
/svelte.config.js
tailwind.config.js file.
- Import the
daisyuiandtypographyplugins - Configure our content paths (you may need to modify these values depending on your project structure)
- Add the
daisyuiplugin after any officialy@tailwindcssplugins (onlytypographyin our example)
/tailwind.config.js
/src/app.postcss
/src/routes/+layout.svelte
/src/routes/+layout.js file to configure our application as only client-side. This means the app will be delivered to the client as unrendered HTML and JavaScript.
/src/routes/+layout.js
Stellar dependencies
To work with the Stellar network, datastructures, and locally stored keypairs, we’re going to install and configure a few more dependencies.Stellar Wallets Kit v1The Stellar Wallets Kit dependency is pinned to
@^1 on purpose. This tutorial and its companion stellar/basic-payment-app are written against the Kit v1 API (new StellarWalletsKit(...)). Installing the package unpinned now resolves to v2.x, whose initialization API is incompatible. This pin keeps the tutorial working until it and BasicPay are migrated to v2 together.window.js file to inject Buffer into our client-side code, since that’s required by some parts of the @stellar/stellar-sdk.
/src/lib/window.js
vite.config.js file.
/vite.config.js