<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fred Allen on Medium]]></title>
        <description><![CDATA[Stories by Fred Allen on Medium]]></description>
        <link>https://medium.com/@fredjourney?source=rss-5ca933327333------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IaxTsMNFn-oR0tpG.jpeg</url>
            <title>Stories by Fred Allen on Medium</title>
            <link>https://medium.com/@fredjourney?source=rss-5ca933327333------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 09:49:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fredjourney/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to build a Phoenix hello world rest API]]></title>
            <link>https://medium.com/@fredjourney/how-to-build-a-phoenix-hello-world-rest-api-5fc9e3acf331?source=rss-5ca933327333------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fc9e3acf331</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix]]></category>
            <dc:creator><![CDATA[Fred Allen]]></dc:creator>
            <pubDate>Sat, 25 Mar 2017 02:32:14 GMT</pubDate>
            <atom:updated>2017-03-25T15:36:36.698Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I went to a <a href="https://www.meetup.com/Elixir-Mexico-City/events/238410497/">Meetup</a> on Mexico City about Elixir, after all the introduction made by <a href="https://medium.com/u/c9707a914188">Norberto Ortigoza</a>, I got impressed about all the expressiveness of the language, their concurrent capacity and how it meets current web development state of art.</p><p>I got a lot of inspiration from that event, so I decided to give it a try for one our most common use case at <a href="https://medium.com/u/4548166bd8bd">Yalo</a>, a Rest API microservice. Many of the articles I found about rendering pure JSON are not up to date and most of them include html views render. So I decided to write this article for my own log and to help others that probably are my current situation.</p><h4>Requirements</h4><ul><li>Elixir &gt; 1.4 and Phoenix running</li></ul><h4>Generate the new Phoenix application</h4><p>First we will generate a new Phoenix application that doesn’t include html assets and database.</p><pre>$ mix phoenix.new hello_phoenix_api --no-brunch --no-ecto</pre><p>You should see and output like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*7HhgWcCQznyPF82zdBi_Kg.png" /><figcaption>new Phoenix app scaffold</figcaption></figure><p>Be sure to install all dependencies typing Y</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*T-6z-cTQ5ePuCfvyjBbIPA.png" /></figure><h4>Create the route, controller and view</h4><p>After the new project generation we’re going to create the route for our hello world endpoint. Open the file /hello_phoenix_api/web/router.ex . The file should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2569062f503422ef47d62133408a2f95/href">https://medium.com/media/2569062f503422ef47d62133408a2f95/href</a></iframe><p>We’re going to remove the /api scope, :browser pipeline and use the root path to render our json payload, the file should be as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3e2ba4c58d50a677033f906908f675f/href">https://medium.com/media/c3e2ba4c58d50a677033f906908f675f/href</a></iframe><p>Now run $ mix phoenix server to start your application and open your browser on <a href="http://localhost:4000/">http://localhost:4000</a> and you should see an error page like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oGLAMidqvPFMAfsiqgNY3Q.png" /></figure><p>It means that you don’t have the appropriate controller that maps to the route.</p><p>Remove the page_controller.ex file because you don’t need it any more.</p><p>Now create your controller file at /hello_phoenix_api/web/controllers/hello_controller.ex with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d13b4d4ca11fcb088ac6da84f3c5017/href">https://medium.com/media/2d13b4d4ca11fcb088ac6da84f3c5017/href</a></iframe><p>Run phoenix server again and you should see this response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o1ILA80ZDa1quCVZVw95uA.png" /></figure><p>Now is time to create the view, in /hello_phoenix_api/web/views/hello_view.ex</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac66d90727ce7ce4eaca1807ecedfdcc/href">https://medium.com/media/ac66d90727ce7ce4eaca1807ecedfdcc/href</a></iframe><p>After you go to <a href="http://localhost:400/">http://localhost:400</a>0 again and refresh the page you will see how the view render a json response</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*PbHPX8AFb_reswocC1h1Mg.png" /></figure><p>Let me know if this is usefull for you or if I have any errors.</p><h4>Unit testing</h4><p>Now lets add a simple unit test to our REST endpoint. First you need to add the <a href="https://github.com/devinus/poison">Poison</a> library which allows to parse JSON objects into maps. Add {:poison, &quot;~&gt;3.0&quot;}to your mix.exs file into the deps list and download the dependency by typing $ mix deps.get in your terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*f1wGbbSpiRpKZ435n3wRFg.png" /></figure><p>Create the file /hello_phoenix_api/test/controllers/hello_controller_test.exs with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0d77380f19f3e5b62d458b6ad6dec3c/href">https://medium.com/media/c0d77380f19f3e5b62d458b6ad6dec3c/href</a></iframe><p>Run your test from the terminal with $ mix test you’ll see an output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*YEXY1YKEJ8AJy43VKZXgKw.png" /></figure><blockquote>fred</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fc9e3acf331" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>