<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Loopback - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Loopback - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 16:32:22 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/loopback/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Build a RESTful API with Authentication in 5 minutes — all from your command line (Part 1) ]]>
                </title>
                <description>
                    <![CDATA[ By Niharika Singh If the title of this article excites you, then my friend, you’re about to achieve level 100 of satisfaction by the end. I’ll quickly go through the course of this article: What we are about to create: RESTful API which handles logs... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-restful-api-with-authentication-under-5-minutes-using-loopback-by-expressjs-no-programming-31231b8472ca/</link>
                <guid isPermaLink="false">66c34673622ca5970af83304</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ authentication ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Loopback ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 27 Jan 2018 10:24:38 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*r9V8K9siyS45bR95DLPQSA.gif" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Niharika Singh</p>
<p>If the title of this article excites you, then my friend, you’re about to achieve <strong>level 100</strong> of satisfaction by the end. I’ll quickly go through the course of this article:</p>
<ol>
<li><strong>What we are about to create:</strong> RESTful API which handles logs of food items on a restaurant menu. The database used in the back-end will be MongoDB. (You can literally use any fricking database on this planet. There’s an exhaustive list of database connectors/non-database connectors supported by <a target="_blank" href="https://loopback.io/">LoopBack</a> below.)</li>
<li><strong>What is LoopBack:</strong> In extremely simple terms, it is highly extensible, open source Node.js framework used to create dynamic, end-to-end REST APIs very rapidly. APIs generated via LoopBack are Swagger APIs (world’s most popular API framework, and you’ll see why very soon). The front-end could be made in whichever framework you’re in love with; Angular or React.</li>
<li><strong>Creating application via CLI:</strong> This is the WOW part which removes all the programming involved. LoopBack CLI is so beautiful that all the hours of development work are reduced down to seconds. Here, we’d be setting up our database using CLI.</li>
<li><strong>Creating data models via CLI:</strong> Again, no programming. All via the beautiful CLI.</li>
<li><strong>Setting up Authentication via CLI:</strong> If you have experience creating APIs, you know how tough it is to restrict parts of API using authentication. Setting up token-based authentication using Express+Node.js on the server side is a pain. All of that pain will be taken away by tasting the elixir of LoopBack! It is heaven’s own drink.</li>
</ol>
<p><img src="https://cdn-media-1.freecodecamp.org/images/iGHE28Ucn5MVvYyBssY3frS7FDU1HIUwo8AX" alt="Image" width="477" height="252" loading="lazy"></p>
<h4 id="heading-step-by-step-guide">Step by Step Guide:</h4>
<p><strong>Pre-requisites:</strong> Make sure you’ve got <a target="_blank" href="https://nodejs.org/en/">Node.js</a>, <a target="_blank" href="https://robomongo.org/">Robomongo</a> installed and MongoDB server running.</p>
<h4 id="heading-step-1-install-loopback-cli-via-npm"><strong>STEP 1: Install LoopBack CLI via NPM</strong></h4>
<p>Open the terminal and write the following command to install LoopBack CLI so that ‘lb’ command can be accessed. Only through ‘lb’ command can we generate applications, models, data sources etc. For further reading: <a target="_blank" href="https://loopback.io/doc/en/lb2/Command-line-tools.html#using-yeoman">https://loopback.io/doc/en/lb2/Command-line-tools.html#using-yeoman</a></p>
<pre><code>$ npm install -g loopback-cli
</code></pre><p>Make sure you install this globally, or else ‘lb’ command might not work for you.</p>
<h4 id="heading-step-2-creating-application"><strong>STEP 2: Creating Application</strong></h4>
<p>Make a directory where you wish to store your project. I’ll name it ‘restaurant-menu’. Make sure you’ve opened this directory in your terminal so that all the files generated via LoopBack are stored in that folder.</p>
<p>Then enter the following command:</p>
<pre><code>$ lb
</code></pre><p>A lot of questions will be asked, like those displayed in the image below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/R0sLAcXrQXqn-zeUSfj9thykmF9YwRcYKCNa" alt="Image" width="800" height="515" loading="lazy">
<em>This is what it should look like.</em></p>
<p>(To navigate among options, use arrow keys on your keyboard)</p>
<h3 id="heading-the-api-is-created"><strong>THE API IS CREATED!</strong></h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/rBQvFJ35WfSz3QdckTEmIqPc6KUZDzftWIBd" alt="Image" width="378" height="203" loading="lazy"></p>
<p>I’m not kidding. Don’t believe me? Run the application using the following command:</p>
<pre><code>$ node .
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/rAYYNopW-YM9SyR51w47dU6VI2lEsF1Rto1U" alt="Image" width="800" height="119" loading="lazy"></p>
<p>If you point to localhost:3000, you’ll see something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/yiv9xKmwl7OkCTdHmolz7o232lczz6KNBB6o" alt="Image" width="712" height="286" loading="lazy">
<em>This will only tell you when the API was started and since how many seconds it has been up.</em></p>
<p>However, if you go to localhost:3000/explorer, you’ll see the gorgeous SwaggerAPI.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/nGKuHXxfQh-B85Ny9MIXh8GiXpyW7nSn6yCN" alt="Image" width="800" height="477" loading="lazy"></p>
<p>LoopBack has set up all the routes for you:</p>
<p>GET users, POST users, PUT users, DELETE users, Login, Log out, Change Password. Literally everything! It would otherwise take hours of work to code this out.</p>
<p>Open this folder in any text editor. I’d be using Atom.</p>
<h4 id="heading-step-3-connecting-mongodb"><strong>STEP 3: Connecting MongoDB</strong></h4>
<p>If you open <code>**datasources.json**</code> in the Server folder, you should see something like:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"db"</span>: {
    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"db"</span>,
    <span class="hljs-attr">"connector"</span>: <span class="hljs-string">"memory"</span>
  }
}
</code></pre>
<p>This means that presently, the data source being used is the memory of our computer. We’ve got to change this to Mongo. So let’s install mongo connector:</p>
<pre><code>$ npm install --save loopback-connector-mongodb
</code></pre><p>Alongside, I hope mongod is running. This is how you’d know it is running:</p>
<pre><code><span class="hljs-number">2018</span><span class="hljs-number">-01</span><span class="hljs-number">-27</span>T15:<span class="hljs-number">01</span>:<span class="hljs-number">13.278</span>+<span class="hljs-number">0530</span> I NETWORK  [thread1] waiting <span class="hljs-keyword">for</span> connections on port <span class="hljs-number">27017</span>
</code></pre><p>Now, let’s connect the connector!</p>
<pre><code>$ lb datasource mongoDS --connector mongoDB
</code></pre><p>This will ask a lot of questions as follows:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/-satN8CMcb23tpBD1cmouXNwjbr4gLG9ArWj" alt="Image" width="800" height="188" loading="lazy"></p>
<p>Now modify <code>**datasources.json**</code> because we don’t wish to use memory. We wish to use Mongo.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"db"</span>: {
    <span class="hljs-attr">"host"</span>: <span class="hljs-string">"localhost"</span>,
    <span class="hljs-attr">"port"</span>: <span class="hljs-number">27017</span>,
    <span class="hljs-attr">"url"</span>: <span class="hljs-string">""</span>,
    <span class="hljs-attr">"database"</span>: <span class="hljs-string">"food"</span>,
    <span class="hljs-attr">"password"</span>: <span class="hljs-string">""</span>,
    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"mongoDS"</span>,
    <span class="hljs-attr">"user"</span>: <span class="hljs-string">""</span>,
    <span class="hljs-attr">"connector"</span>: <span class="hljs-string">"mongodb"</span>
  }
}
</code></pre>
<p>So our database named: <code>**food**</code> is created.</p>
<h4 id="heading-step-4-creating-data-models"><strong>STEP 4: Creating Data Models</strong></h4>
<p>Run following command to create data models:</p>
<pre><code>$ lb model
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/rhCkwWcP7f1SiI5ezMAvtdHhXU6eUllSB9pZ" alt="Image" width="800" height="728" loading="lazy"></p>
<p>You may add however many properties to a particular model. To stop entering more properties, just hit Enter to get out of the CLI.</p>
<p>Check out <code>**dishes.json**</code> in the Common/Models folder.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"dishes"</span>,
  <span class="hljs-attr">"base"</span>: <span class="hljs-string">"PersistedModel"</span>,
  <span class="hljs-attr">"idInjection"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"options"</span>: { <span class="hljs-attr">"validateUpsert"</span>: <span class="hljs-literal">true</span> },
  <span class="hljs-attr">"properties"</span>: {
    <span class="hljs-attr">"name"</span>: { <span class="hljs-attr">"type"</span>: <span class="hljs-string">"string"</span>, <span class="hljs-attr">"required"</span>: <span class="hljs-literal">true</span> },
    <span class="hljs-attr">"price"</span>: { <span class="hljs-attr">"type"</span>: <span class="hljs-string">"number"</span>, <span class="hljs-attr">"required"</span>: <span class="hljs-literal">true</span> }
  },
  <span class="hljs-attr">"validations"</span>: [],
  <span class="hljs-attr">"relations"</span>: {},
  <span class="hljs-attr">"acls"</span>: [],
  <span class="hljs-attr">"methods"</span>: {}
}
</code></pre>
<p>You may edit the properties from this json file as well. It is not necessary to use CLI.</p>
<p>Now let’s rerun the server using the following command and head over to localhost:3000/explorer</p>
<pre><code>$ node .
</code></pre><p>Now you’ll see 2 models: <code>**dishes**</code>, and <code>**user**</code></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/yiCcXdhwh6KmkvQ65H6KHvjskh9pbc9JbXbr" alt="Image" width="800" height="207" loading="lazy"></p>
<p>Now let’s POST some <code>dish</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/bncjurriEZN0SKiIdb7POrdOAocWlFjNAQHR" alt="Image" width="642" height="1120" loading="lazy"></p>
<p>Now let’s GET the same <code>dish</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/38k0M-rkWNr5-x1UDVtS0L7w9IDFWd0ucxlv" alt="Image" width="640" height="1042" loading="lazy"></p>
<p>You may play around with other HTTP requests too!</p>
<p>These APIs can be accessed outside the explorer as well:</p>
<p><a target="_blank" href="http://localhost:3000/api/dishes">http://localhost:3000/api/dishes</a></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/w-Hiil7wjDNHghvGoW488BgOsHwBzgVEDs2T" alt="Image" width="678" height="402" loading="lazy"></p>
<h4 id="heading-step-5-authentication-cherry-on-the-cake"><strong>STEP 5: AUTHENTICATION: Cherry on the cake!</strong></h4>
<p>To set up authentication, run the following command:</p>
<pre><code>$ lb acl
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/bd94tNvvyNQzynur9S9CKyC-lSKoU8IKR2ye" alt="Image" width="800" height="183" loading="lazy"></p>
<p>Now, let’s try to GET the <code>**dishes**</code>. Before that, please rerun the server.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/BeN7OxZjcmu7EcRwPvf0PRab8HRavKLx3ya9" alt="Image" width="642" height="1078" loading="lazy">
<em>So it doesn’t GET any dishes. As expected. Because we are not logged in. It alerts us by saying Authentication required.</em></p>
<p>Let’s get authenticated! For that, we need to get registered first. So we POST in <code>**users**</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/IhYOhlb41zpLxsKOtVzX37Ji7dVriLKDv2Ln" alt="Image" width="642" height="1198" loading="lazy"></p>
<p>Now, let’s log in.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/hxCQm4xGEBEvevqPZtL6sgVN1mXoRV5tpj2u" alt="Image" width="644" height="1214" loading="lazy"></p>
<p>Now, copy the ID in the response body and paste it in the Access Token field on top of the page.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/NY830CQpK2jD-BMJ0gvbDAadX1KOGyC8fh-r" alt="Image" width="800" height="41" loading="lazy"></p>
<p>Now we are authenticated. YAY.</p>
<p>Now, let’s GET the <code>**dishes**</code> again.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/DhkE4UiURk9UHLXBvwWBLlM2PoWjeZuWc5dM" alt="Image" width="638" height="1044" loading="lazy"></p>
<p>HOORAY!</p>
<p>Congratulations if you’ve successfully reached this step. So proud of you.</p>
<p>Next steps would be to create a front end around this API which would be done later.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/cJRJuth-oFUh34e3LYZXnP2dl75mFx3jHuPf" alt="Image" width="500" height="281" loading="lazy"></p>
<h4 id="heading-the-frontend-tutorial-of-this-article-can-be-found-herehttpsmediumfreecodecamporghow-to-build-a-restful-api-with-authentication-in-5-minutes-all-from-your-command-line-part-2-dcf29d5de0bb-in-that-tutorial-i-have-used-reactjs-to-weave-a-frontend-around-this-api">The frontend tutorial of this article can be found <a target="_blank" href="https://medium.freecodecamp.org/how-to-build-a-restful-api-with-authentication-in-5-minutes-all-from-your-command-line-part-2-dcf29d5de0bb">here</a>. In that tutorial, I have used ReactJS to weave a frontend around this API.</h4>
<p>Bye folks!<br>Happy coding.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
