Create Ethereum API Services with Parity & Django Rest Framework

Dr. Akando
Oct 18, 2017 · 3 min read

Making a REST API Services will help you to connect any clients of choice to the Ether Network: Chrome Extensions, Mac App, iOS App or Android App.

I am personally building Swiftly — an iMessage Extensions for Ethereum so I need a REST API to interact with the network.

1. Install Parity and Its Dependencies

Install Rust

$ curl https://sh.rustup.rs -sSf | sh

Install and Build Parity inside Digital Ocean Droplet

$ bash <(curl https://get.parity.io -Lk)

Sync with the Ethereum network

$ parity

2. Config Parity for Digital Ocean

Create Parity .service file to start and restart when Ethereum node is down.

$ cd /etc/systemmd/system
$ vim parity.service

Parity.service should look like this

If you want to use additional configuration for Parity, you can use Parity Generator and put your config.toml this directory

$ cd /root/.local/share/io.parity.ethereum
$ vim config.toml

The ‘config.toml’ should look like this with your own additional configuration.

Now lets update our changes and start parity.service

$ systemctl daemon-reload      // Reload Daemon
$ service parity start // Start Parity
$ service parity status // Check if the Service running
$ top // Check CPU & Memory Usage of Parity

I also put all my Parity Ethereum Node inside a separate 100G Digital Ocean Block Storage since you need a least 40G and some additional disk space in future.

Now lets test interacting with the main network using RPC APIs using Curl and the eth_getBalance API. You can read more about other available APIs here.

$ curl --data '{"method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1"],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545

3. Write REST API Services using Django and Django Rest Framework

$ cd home/django/django_project
$ pip install djangorestframework coreapi
$ python manage.py startapp api
$ python manage.py migrate

And then we need to edit settings.py file inside django_project folder, add our app api, rest_framework into INSTALLED_APPS.

Create Transaction models inside our Database

We need a Serializer class to convert our data into json format for our APIs.

Now we can write our Views to display APIs endpoints and accept parameters from our future requests.

Edit our urls.py inside django_project folder to create our APIs endpoints.

OK now we just need our views to interact with RPC APIs. After this, we need migrate our database, test APIs with Postman and we are ready to go.

$ python manage.py makemigrations
$ python manage.py migrate
$ service nginx restart

References:

About the author: Troy DO is an iOS and Machine Learning Consultant. He has more than 4 years experience building and launching mobile and web products. Recently, he has been working with a lot of AI-related projects in the area of Healthcare and Financial Technology with his team at Top Flight Apps.

Engineer Infinite Value from Finite Resources

The Key Engine Of Infinite Economic Growth Last Few…

Engineer Infinite Value from Finite Resources

The Key Engine Of Infinite Economic Growth Last Few Centuries Lays in The Ability of Engineers, Scientists and Entrepreneurs to Turn Our Limited Resources into Products and Services Enabling Human Civilization to Explore, Create and Build Ever Better Future

Dr. Akando

Written by

Writer and Entrepreneur. Interested in Computer Vision, Geo Politics and Painting.

Engineer Infinite Value from Finite Resources

The Key Engine Of Infinite Economic Growth Last Few Centuries Lays in The Ability of Engineers, Scientists and Entrepreneurs to Turn Our Limited Resources into Products and Services Enabling Human Civilization to Explore, Create and Build Ever Better Future

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store