{"id":2518,"date":"2026-08-03T12:20:35","date_gmt":"2026-08-03T05:20:35","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/08\/03\/how-to-create-a-simple-api-with-express-a-step-by-step-guide\/"},"modified":"2026-08-03T12:20:35","modified_gmt":"2026-08-03T05:20:35","slug":"how-to-create-a-simple-api-with-express-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/08\/03\/how-to-create-a-simple-api-with-express-a-step-by-step-guide\/","title":{"rendered":"How to Create a Simple API with Express: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Create a Simple API with Express: A Step-by-Step Guide<\/h1>\n<p>Building a backend doesn&#8217;t have to be complicated. Express, a lightweight Node.js framework, lets you create a fully functional JSON API in minutes. This guide walks you through the essential steps to make a simple API with Express.<\/p>\n<p>Before you begin, make sure Node.js and npm are installed on your machine. Open your terminal, create a project folder, and run <code>npm init -y<\/code> to generate a package.json file. Then install Express with <code>npm install express<\/code>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20create%20a%20simple%20api%20with%20express\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Step 1: Set Up Your Server<\/h2>\n<p>Create an <code>index.js<\/code> file in your project root. This is where you&#8217;ll define your Express app. Write the following setup at the top of the file:<\/p>\n<p><code>const express = require('express'); const app = express();<\/code><\/p>\n<p>You&#8217;ll also need to parse incoming JSON. Add <code>app.use(express.json())<\/code> to enable middleware that reads request bodies. This is essential for handling POST requests.<\/p>\n<h2>Step 2: Define Your Routes<\/h2>\n<p>Routes determine how your API responds to requests. For a simple API, you can start with a few key endpoints. Here&#8217;s how to build them with the <code>app.get()<\/code> and <code>app.post()<\/code> methods.<\/p>\n<ul>\n<li><code>app.get('\/api\/ping')<\/code> \u2014 returns a simple &#8220;pong&#8221; response.<\/li>\n<li><code>app.post('\/api\/data')<\/code> \u2014 accepts JSON and sends back a confirmation.<\/li>\n<li><code>app.get('\/api\/users')<\/code> \u2014 returns a small list of sample data.<\/li>\n<\/ul>\n<p>Each route takes a request and response argument, which you can use to read parameters and send back JSON with <code>res.json()<\/code>.<\/p>\n<h2>Step 3: Start and Test the API<\/h2>\n<p>To listen for requests, add a final line that starts your server. Using <code>app.listen(3000, () => console.log('API running on port 3000'))<\/code> will start the server. Run <code>node index.js<\/code> in your terminal to confirm it works.<\/p>\n<p>Then, open your browser and navigate to <code>http:\/\/localhost:3000\/api\/ping<\/code>. You should see the JSON response. Alternatively, use a tool like Postman or curl to test your POST endpoint with sample data.<\/p>\n<h2>Conclusion<\/h2>\n<p>You&#8217;ve just built a simple API with Express. From here, you can extend it with a database, authentication, and more complex routing logic to turn it into a full-featured backend.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Create a Simple API with Express: A Step-by-Step Guide Building a backend doesn&#8217;t have to be complicated. Express, a lightweight Node.js framework, lets you create a fully functional JSON API in minutes. This guide walks you through the essential steps to make a simple API with Express. Before you begin, make sure Node.js &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2518","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=2518"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2518\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}