πΎBackend Integration Guide
π‘ Integration guide for an application where the user has no direct contact with the blockchain, and that requires backend interactions with the blockchain.
In the following, we will show you how to build a REST API using Express.js to enable the framework-agnostic communication with your newly deployed smart contracts. This API serves as an intermediary, enabling your backend system of any framework or programming language to easily interact with your newly deployed smart contracts.
Step 1: Setup Project
Initialize a new Node.js project and install required packages.
Step 2: Configure Environment
Create a .env
file in your project root and add the following:
Step 3: Initialize Express and Web3
In index.js
, set up Express, Web3, and your contract.
Step 4: Define API Endpoints
Implement endpoints to interact with each function of your smart contract.
Step 5: Deploying your REST API
Once you have defined all endpoints you need, deploy your API on the backend hosting service of your choice.
In the following, we show the process for a deployment on Heroku.
Prerequisites:
Heroku CLI installed on your computer.
A Heroku account.
1. Install and Login to Heroku CLI
If you haven't already, download and install the Heroku CLI from Heroku's website. Once installed, open your terminal and log in:
This will open a web browser for you to log in to your Heroku account.
2. Initialize a Git Repository
Navigate to your project directory and initialize a Git repository if you havenβt done so:
3. Create Heroku App, optionally with Custom Name and Region
Create a new Heroku application with your preferred name and region. Replace your-app-name
with the desired name and choose either us
or eu
for the region:
4. Set Environment Variables in Heroku
Configure the environment variables required by your application directly in Heroku. Ensure you replace these placeholder values with your actual configuration details:
5. Deploy to Heroku
Deploy your application to Heroku by pushing your code:
6. Verify the Deployment
After the deployment process completes, ensure that your application is running correctly:
This command shows real-time logs from your Heroku app, which is useful for troubleshooting.
Troubleshooting Common Issues
Incorrect Commands: Verify that all commands are executed correctly without typos.
Environment Variables: Ensure all environment variables are set accurately on Heroku.
Heroku Logs: Check the Heroku logs for detailed error information if you encounter issues.
Resource Limitations: Sometimes, the selected region may have limitations. If you face deployment issues, try switching the region or reach out to Heroku support for assistance.
Step 6: Integrating your API with Existing Web2 Systems
After the deployment was successful, we integrate the REST API with your existing Web2 systems to interact with the smart contract.
Utilize HTTP clients like axios
or fetch
our similar services for your programming language and framework of choice in your backend to call our freshly deployed REST API:
Conclusion
With this setup, you've created a fully functional Express.js REST API that interacts with your smart contracts. This API enables your existing backend systems to interact with blockchain technology seamlessly. Remember to test thoroughly before deploying to production. And if you have any questions, please donβt hesitate to reach out to us via developers@onino.io!
Last updated