This post jumps ahead a little bit, but I want to note down all the steps while they're fresh in my head. And I'll be honest here, I'm not a fan of packages and dependencies and anything that involves too much fiddling around in the Terminal. I fear things which put their hooks in and risk leaving behind jellyfish-like barbs when there's any attempt to uninstall or update.
But I decided to be brave because I could see the benefits this time and Node.js looked like it was the simplest route I could take. If you decide you'd like to try before you buy, then you can play with the SDK inside RunKit without any installations before taking the leap, or else charge headlong like me into Node.js.
Step 1: Installing Node Version Manager (NVM)
The Stellar SDK recommends Node version 10.16.3, which is one behind the current stable release and having done a bit of reading, I realised that uninstalling Node from macOS can be a bit messy, so I opted to follow the advice to install Node Version Manager (NVM), to make updating and uninstalling Node and Node Package Manager (NPM) less painful.
First snag I hit was that the default shell for macOS is now zsh not bash. Solution, install with the following command:
curl raw.github.com/creationix/nvm/master/install.sh | zshFor install on a Linux server running bash, use:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
Step 2: Installing Node 10.16.3
With the version manager installed, I could now install the correct version of node:
nvm install 10.16.3
Step 3: Time for a "Hello World"
Before getting any more complicated. Save this code to a file called HelloWorld.js
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Once you've done that go to the Terminal and cd into the directory where your file is saved. Now type:
node HelloWorld.js
You should see the "Server running at ..." text. Now proceed to Step 4, else troubleshoot to see where you're going wrong.
Step 4: Installing the Stellar SDK
With Node installed create a directory for your app, then cd to the directory in Terminal. Now we use the Node Package Manager to install the Stellar SDK into this directory.
npm install --save stellar-sdk
Step 5: First API call with Stellar SDK
We're going to retrieve the details of a ledger as a test. In order to do this, save this code to a file called stell.js (in the same directory that you installed the Stellar SDK in):
var StellarSdk = require("stellar-sdk"); var server = new StellarSdk.Server("https://horizon.stellar.org"); server .ledgers() .ledger("69858") .call() .then(function (resp) { console.log(resp); }) .catch(function (err) { console.error(err); });
Now go to the Terminal and from within the same directory, type
node stell.js
You should receive a JSON response, which contains details of the ledger requested.
Step 6
There is no Step 6, I'll move on to more complex calls like payments and buy and sell offers in another post.
Comments
Post a Comment