How to create a JSON REST API for Ethereum
Use ExpressJS to create a server with endpoints for HTTP verbs. Parse JSON input with app.use() and test with axios library.
Use ExpressJS to create a server with endpoints for HTTP verbs. Parse JSON input with app.use() and test with axios library.
Letβs create our own JSON REST API for Ethereum!
Essentially, this means weβll create a server that will respond to certain HTTP verbs:
- GET - Get a resource or a list of resources
- POST - Create a new resource
- PUT - Update a resource
- DELETE - Delete a resource
Setting Up π¦
Create a new folder for this project. Call it whatever you like!
Once youβve created the folder, navigate to it in the terminal and create a package.json. You can do so with:
ππΌ This will create a new package.json where we can install all our dependencies. Next, install expressjs. This library will help us quickly spin up REST endpoints:
The Server π»
Letβs create the server now! Create a new folder inside the project called server and add an index.js inside of it.
Afterward, your directory structure should look like this:
Now inside the index.js file, letβs create our REST API. First, letβs try to respond to a simple GET request.
The GET Request
Letβs get a simple βHello Worldβ example working. Copy the following code into your index.js file:
ππΌ This will respond to a GET request at the base route / with the raw string βHello Worldβ. You can test this out by running the server with node:
// eslint-disable-next-line no-unused-vars
This should log "Listening at http://localhost:3000". If you go to that localhost URL you should find the message βHello Worldβ waiting for you.
A tip! Use the nodemon package to run your server and you wonβt need to restart the server every time you change your code. Nodemon will do this for you!
Next, letβs change what weβre returning in app.get to be some resource:
ππΌ Now we should return things instead of βHello Worldβ. Test it out!
The POST Request
Next, letβs create a thing!
First, weβll need to add an endpoint to our server which will take user input to create a new thing.
In addition to our app.get endpoint, letβs add an app.post endpoint:
ππΌ This POST endpoint will allow us to take the body of the request and push it onto our things array. Then it will send a response with all of the things included.
Be sure to include the app.use line. This is using express middleware which will know how to properly handle a JSON request.
OK, now we have our POST endpoint set up. Letβs make a request to our server!
Making the Request
Next, letβs create a new top-level folder called scripts and add a create.js inside of it. Your directory structure should look like this afterward:
Letβs install the axios library to easily run our POST request:
Inside of this create.js letβs add the following code:
ππΌ Here we are creating a new thing and POSTing it to our server running at port 3000.
For this step, youβll need to keep the server running on port 3000 while also running the create.js script. Navigate to the /scripts folder in your terminal and run:
Did it work? If it did, you should have created a new thing and received in the response. Run it again, and you get two things! π
Challenge 1: Customize π¨
Customize the REST API. Can you turn the thing resource into something you would actually want to create a server around?
You could use the dictionary example from our first week, or TODOs, or a game, or virtual currency! Whatever you like, this is your REST API!
Be sure to give the resource some additional properties other than a name!
Challenge 2: DELETE Request βοΈ
For this challenge, you will need to implement a DELETE method on the API.
The express library has a delete method on the app which you can use to create the endpoint on the server.
Then you will want to create a new script that you will use to delete one of the things on the server. Axios also has a delete method you can use to run the request.
Something to think about: how should you identify the resource? π€
Perhaps each resource could have a specific id property to identify it by! π‘
Every time you restart the server, the variables stored in memory will be re-initialized unless you store it to your file system or some other kind of database.
Challenge 3: PUT Request βοΈ
For this challenge, you will need to implement a PUT method on the API. The PUT method should update a particular resource. Once again, youβll want a way to identify which resource you are referring to so the server knows which one to update!
The express library has a put method on the app which you can use to create the endpoint on the server.
Then you will want to create a new script that you will use to delete one of the things on the server. Axios also has a put method you can use to run the request.
Then you will want to create a new script that you will use to update one of the things on your server.
Learn More About Ethereum
Alchemy University offers free web3 development bootcamps that explain Ethereum in-depth and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!