<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[API Endpoint using BUN]]></title><description><![CDATA[API Endpoint using BUN]]></description><link>https://api-endpoint-bun.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 05:19:03 GMT</lastBuildDate><atom:link href="https://api-endpoint-bun.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Bun - API Creater]]></title><description><![CDATA[Bun: The Fast, All-in-One JavaScript Runtime 🚀
With the growing ecosystem of JavaScript runtime environments, Bun is gaining attention as a fast, modern alternative to Node.js and Deno. Designed for speed and developer productivity, Bun provides bui...]]></description><link>https://api-endpoint-bun.hashnode.dev/bun-api-creater</link><guid isPermaLink="true">https://api-endpoint-bun.hashnode.dev/bun-api-creater</guid><category><![CDATA[Bun]]></category><category><![CDATA[APIs]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[Aryan Vekariya]]></dc:creator><pubDate>Thu, 23 Jan 2025 05:15:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737606966530/8f0808fb-be50-4ae0-9736-0dd1719c3902.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-bun-the-fast-all-in-one-javascript-runtime">Bun: The Fast, All-in-One JavaScript Runtime 🚀</h2>
<p>With the growing ecosystem of JavaScript runtime environments, <strong>Bun</strong> is gaining attention as a fast, modern alternative to <strong>Node.js</strong> and <strong>Deno</strong>. Designed for speed and developer productivity, Bun provides built-in tools like a JavaScript runtime, bundler, and task runner—all in one package. This makes it a great choice for building APIs quickly and efficiently. In this blog, we’ll walk you through creating an API endpoint using Bun.</p>
<hr />
<h3 id="heading-what-is-bun">What Is Bun? 🤔</h3>
<p>Bun is a new JavaScript runtime designed with the following goals:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Goal</td><td>Explanation</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Speed</strong> 🏎️</td><td>Bun is optimized for fast startup times and runtime performance.</td></tr>
<tr>
<td><strong>Developer-friendly</strong> 💻</td><td>It includes features like TypeScript support, a built-in package manager, and a fast bundler.</td></tr>
<tr>
<td><strong>All-in-one solution</strong> 🔧</td><td>Unlike Node.js, you don’t need separate tools for package management, transpilation, or runtime execution.</td></tr>
</tbody>
</table>
</div><p>If you’re already familiar with <strong>Node.js</strong> or <strong>Deno</strong>, picking up <strong>Bun</strong> will feel natural.</p>
<hr />
<h3 id="heading-why-use-bun">Why Use Bun? 🔍</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Feature</strong></td><td><strong>Bun's Benefit</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Blazing Fast Performance</strong> ⚡</td><td>Bun is optimized for speed, making it ideal for fast startups and high-performance applications.</td></tr>
<tr>
<td><strong>Built-in Package Manager</strong> 🛠️</td><td>No need for npm or Yarn. Bun’s built-in package manager is faster and handles everything.</td></tr>
<tr>
<td><strong>Support for TypeScript</strong> 🌟</td><td>TypeScript is supported out-of-the-box, no additional configuration required.</td></tr>
<tr>
<td><strong>All-in-One Toolset</strong> 🔥</td><td>Bun integrates runtime, bundler, and task runner, eliminating the need for multiple external tools.</td></tr>
<tr>
<td><strong>API Creation Made Easy</strong> 📡</td><td>With Bun’s simple HTTP module, building API endpoints is fast and effortless.</td></tr>
<tr>
<td><strong>Zero Configuration</strong> 🏠</td><td>Simply install Bun and you’re good to go—no complex setup needed.</td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-prerequisites">Prerequisites 🛠️</h3>
<p>Before we dive in, make sure you have:</p>
<ul>
<li><p><strong>Bun installed</strong> on your machine:</p>
<pre><code class="lang-plaintext">  bashCopyEditcurl https://bun.sh/install | bash
</code></pre>
</li>
<li><p><strong>Basic knowledge</strong> of JavaScript and HTTP concepts.</p>
</li>
<li><p>A <strong>code editor</strong> (e.g., VS Code).</p>
</li>
</ul>
<hr />
<h3 id="heading-step-1-initialize-a-new-bun-project">Step 1: Initialize a New Bun Project 🔥</h3>
<p>Create a new Bun project. Open your terminal and run:</p>
<pre><code class="lang-plaintext">bashCopyEditbun init api-example
cd api-example
</code></pre>
<p>The <code>bun init</code> command will scaffold a new Bun project with a basic structure and a <code>package.json</code> file.</p>
<hr />
<h3 id="heading-step-2-install-dependencies-optional">Step 2: Install Dependencies (Optional) 📦</h3>
<p>Bun has a built-in package manager that is faster than npm or Yarn. If your API requires external dependencies, install them using Bun. For example:</p>
<pre><code class="lang-plaintext">bashCopyEditbun add express
</code></pre>
<p>However, <strong>Bun</strong> can handle HTTP requests without additional libraries, thanks to its built-in APIs.</p>
<hr />
<h3 id="heading-step-3-create-your-api-server">Step 3: Create Your API Server 🚀</h3>
<p>Bun’s HTTP module makes creating a server easy. Create a file named <code>server.js</code> (or <code>server.ts</code> for TypeScript). Inside, write the following code:</p>
<pre><code class="lang-plaintext">javascriptCopyEditimport { serve } from "bun";

serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    const path = url.pathname;

    // Define routes
    if (path === "/") {
      return new Response(JSON.stringify({ message: "Welcome to the Bun API!" }), {
        headers: { "Content-Type": "application/json" },
      });
    } else if (path === "/hello") {
      return new Response(JSON.stringify({ message: "Hello, World!" }), {
        headers: { "Content-Type": "application/json" },
      });
    }

    // Handle 404
    return new Response(JSON.stringify({ error: "Not Found" }), {
      status: 404,
      headers: { "Content-Type": "application/json" },
    });
  },
});
</code></pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><p><code>serve</code>: Bun’s built-in function to start an HTTP server.</p>
</li>
<li><p><code>fetch</code>: Handles incoming HTTP requests.</p>
</li>
<li><p><code>req.url</code>: The URL of the request, which is used to define simple routing logic.</p>
</li>
<li><p><code>Response</code>: The object returned to the client.</p>
</li>
</ul>
<hr />
<h3 id="heading-step-4-start-the-server">Step 4: Start the Server 🏃‍♂️</h3>
<p>To start the server, run the following command:</p>
<pre><code class="lang-plaintext">bashCopyEditbun server.js
</code></pre>
<p>Bun will start your server at <a target="_blank" href="http://localhost:3000"><code>http://localhost:3000</code></a>. You should see output indicating that the server is running.</p>
<hr />
<h3 id="heading-step-5-test-your-api">Step 5: Test Your API 🔍</h3>
<p>Test your API endpoints using tools like <strong>Postman</strong>, <strong>cURL</strong>, or a <strong>browser</strong>.</p>
<p>For example:</p>
<ul>
<li><p>Navigate to <a target="_blank" href="http://localhost:3000%EF%BF%BCResponse"><code>http://localhost:3000</code><br />  Response</a>: <code>{ "message": "Welcome to the Bun API!" }</code></p>
</li>
<li><p>Navigate to <a target="_blank" href="http://localhost:3000/hello%EF%BF%BCResponse"><code>http://localhost:3000/hello</code><br />  Response</a>: <code>{ "message": "Hello, World!" }</code></p>
</li>
<li><p>Navigate to a non-existent route like <a target="_blank" href="http://localhost:3000/unknown%EF%BF%BCResponse"><code>http://localhost:3000/unknown</code><br />  Response</a>: <code>{ "error": "Not Found" }</code></p>
</li>
</ul>
<hr />
<h3 id="heading-step-6-enhance-your-api">Step 6: Enhance Your API 🔧</h3>
<p>Want to extend your API? Here are a few ideas:</p>
<ul>
<li><p><strong>Add query parameters</strong>:<br />  Use <code>url.searchParams.get("key")</code> to access query parameters.</p>
</li>
<li><p><strong>Handle POST requests</strong>:<br />  Check <code>req.method === "POST"</code> and use <code>await req.json()</code> to parse JSON bodies.</p>
</li>
<li><p><strong>Use middleware</strong>:<br />  Create reusable functions for tasks like authentication or logging.</p>
</li>
</ul>
<p>Example for handling a POST request:</p>
<pre><code class="lang-plaintext">javascriptCopyEditif (path === "/submit" &amp;&amp; req.method === "POST") {
  const body = await req.json();
  return new Response(JSON.stringify({ received: body }), {
    headers: { "Content-Type": "application/json" },
  });
}
</code></pre>
<hr />
<h3 id="heading-advantages-of-using-bun-for-apis">Advantages of Using Bun for APIs 🚀</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Advantage</strong></td><td><strong>Why It Matters</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Speed</strong> ⚡</td><td>Bun is optimized for blazing fast performance, ensuring quick startups and responsiveness for APIs.</td></tr>
<tr>
<td><strong>Simplicity</strong> 🧑‍💻</td><td>With built-in tools like <code>serve</code>, you don’t need external libraries for basic tasks—making development faster and more streamlined.</td></tr>
<tr>
<td><strong>TypeScript Support</strong> 🌟</td><td>Native TypeScript support allows you to build with types right out of the box.</td></tr>
<tr>
<td><strong>Integrated Ecosystem</strong> 🌍</td><td>No need for multiple tools—everything you need for runtime, bundling, and task running is built into Bun.</td></tr>
<tr>
<td><strong>Small and Lightweight</strong> 🧳</td><td>Bun is designed to be lightweight, reducing memory consumption and improving performance.</td></tr>
<tr>
<td><strong>Zero Configuration</strong> 🔧</td><td>Start building your API with minimal setup—Bun takes care of the rest.</td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-use-cases-of-bun">Use Cases of Bun 🌱</h3>
<ul>
<li><p><strong>API Development</strong> 📡: Quickly spin up and manage RESTful APIs for your applications.</p>
</li>
<li><p><strong>Prototyping</strong> 🔄: Ideal for fast prototyping with its simplicity and speed.</p>
</li>
<li><p><strong>Microservices</strong> 🐝: Build fast, lightweight microservices that can scale efficiently.</p>
</li>
<li><p><strong>Static Site Generation</strong> 📑: Leverage Bun’s bundler for static site generation that’s lightning fast.</p>
</li>
</ul>
<hr />
<h3 id="heading-conclusion">Conclusion 🎉</h3>
<p>Building APIs with Bun is straightforward and highly efficient, thanks to its modern design and built-in features. Whether you’re prototyping a small app or developing a large-scale API, Bun provides the tools to get started quickly. Try it out today and experience the speed and simplicity for yourself! 🚀</p>
]]></content:encoded></item></channel></rss>