Writing Your First RESTful API
The hello world of APIs
Last updated: 2/28/2025
Writing Your First RESTful API - "Hello World"
π Introduction
Welcome to your first RESTful API project! In this lesson, youβll create a basic API with a single GET endpoint (/hello
) that responds with "Hello World"
and a 200 OK status.
This project introduces you to:
- Setting up a basic server.
- Defining routes and handling requests.
- Sending HTTP responses.
π Project Requirements
Your API should:
β
Have a GET endpoint at /hello
.
β
Respond with a 200 OK status.
β
Return the text "Hello World"
.
β
Run on port 3033.
You can use any backend framework or language (Node.js, Python, Java, etc.).
π Setting Up Your API (Node.js + Express Example)
Weβll use Node.js and Express, but you can use any backend framework of your choice.
Step 1: Create a New Project
Open a terminal and run:
mkdir hello-world-api cd hello-world-api npm init -y
Step 2: Install Express.js
npm install express
Step 3: Create index.js
Now, create a new file called index.js
and add this code:
const express = require('express'); const app = express(); // Define the /hello route app.get('/hello', (req, res) => { res.status(200).send('Hello World'); }); // Start the server on port 3033 const PORT = 3033; app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });
π Running Your Server
Start the API by running:
node index.js
If successful, you should see:
Server running on http://localhost:3033
π Testing Your API
β 1οΈβ£ Using a Web Browser
Open http://localhost:3033/hello in your browser.
You should see:
Hello World
β 2οΈβ£ Using cURL (Command Line)
Run:
curl -X GET http://localhost:3033/hello
Expected output:
Hello World
β 3οΈβ£ Using Postman
- Open Postman.
- Create a GET request to
http://localhost:3033/hello
. - Click Send.
- You should see "Hello World" in the response.
π Pushing Your Project to GitHub
To submit your project for evaluation, you need to upload it to GitHub.
Step 1: Create a GitHub Repository
- Go to GitHub.
- Click New Repository.
- Name it hello-world-api.
- Select Public or Private (based on your preference).
- Click Create Repository.
Step 2: Initialize Git Locally
Run the following commands in your project directory:
git init git add . git commit -m "Initial commit - Hello World API"
Step 3: Link Your GitHub Repository
Replace <your-github-username>
with your actual username:
git remote add origin https://github.com/<your-github-username>/hello-world-api.git git branch -M main git push -u origin main
Step 4: Verify the Push
Go to your GitHub repository and confirm that the files are uploaded.
π― Summary
- We created a basic REST API using Express.js.
- The API has a GET route at
/hello
that returns"Hello World"
with a 200 OK response. - The server runs on port 3033.
- We tested the API using a browser, cURL, and Postman.
- We uploaded the project to GitHub for submission.
β Next Lesson: Submitting Your Project for Evaluation
In the next lesson, which is the very first project in this learning track, you will submit the project you just implemented right now to be evaluated and receive the verdict.