<?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 Augusto Cezar on Medium]]></title>
        <description><![CDATA[Stories by Augusto Cezar on Medium]]></description>
        <link>https://medium.com/@augusto1997?source=rss-57682d9e3f94------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vR1rpsdowiN_Hrp3l7WBIg.jpeg</url>
            <title>Stories by Augusto Cezar on Medium</title>
            <link>https://medium.com/@augusto1997?source=rss-57682d9e3f94------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 11:27:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@augusto1997/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[Elixir: Building a REST API using Phoenix Framework Part I]]></title>
            <link>https://medium.com/red-ventures-br-tech/elixir-building-a-rest-api-using-phoenix-framework-part-i-fa3a0a23b574?source=rss-57682d9e3f94------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa3a0a23b574</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Augusto Cezar]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 13:53:38 GMT</pubDate>
            <atom:updated>2020-06-16T17:47:21.919Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DwhOAwifj4VlAHzX" /><figcaption>Photo by <a href="https://unsplash.com/@zanilic?utm_source=medium&amp;utm_medium=referral">Zan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What is Elixir and Phoenix Framework ?</h3><p>Elixir it’s a pure dynamic functional language and as any kind of functional language it does have support for First-Class Functions, Currying, Pattern Matching and data is treated as immutable. It runs on top of <strong>ErlangVM (BEAM), </strong>to give you some context is the same virtual machine that<strong> Whats App</strong> Server from <strong>Facebook </strong>runs on top<strong>.</strong></p><p>Phoenix it’s a Framework that gives you everything you need to start your project. Requires almost zero configuration, which means you don’t need to worry about setup a library to interact with database layer, router to handle the requests or even have to configure a test runner. Also, it does have a Command Line Interface integrated to generate Migrations, Schemas, Controllers and Views.</p><h3>Getting Started</h3><p>As requirement to you follow this guide, you must have <strong>Elixir</strong> with <strong>Mix tool</strong> installed. In case don’t have it, you can follow this <a href="https://elixir-lang.org/install.html">guide</a> of how install in your machine.</p><p>Another dependency but this time not required, it’s <strong>docker </strong>with<strong> docker-compose </strong>tool<strong>. </strong>This dependencies are not required because they will be used to run the <strong>PostgreSQL</strong> database instance, so you can simply run the instance directly from your machine instead. The following image refers to <strong>docker-compose </strong>example file for postgres instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcOMm04_Uz0Azv-eKRI0oQ.png" /><figcaption>docker-compose.yml file example for postgres instance</figcaption></figure><h3>Installing Phoenix Framework CLI</h3><p>After you’ve completed the installation of <strong>Elixir</strong> and <strong>Mix</strong>, now let’s install <strong>Phoenix</strong> <strong>CLI</strong> which is a <strong>Command Line</strong> tool for help scaffold <strong>Phoenix</strong> framework projects. To install it’s pretty simple you’ll just need open an terminal window anywhere in your machine and type the following command</p><pre>mix local.hex &amp;&amp; mix archive.install hex phx_new 1.4.16</pre><p>The command above will install hex, or updates if you already have it installed and finally get all the <strong>Phoenix CLI</strong> related dependencies globally.</p><h3>Creating an Web API Based Project</h3><p>To create Web API based project, you can simply type the following command below from a terminal window where you wants to that the project gets created.</p><pre>mix phx.new [project_name] --no-html --no-webpack</pre><p>The CLI tool, it will create a Phoenix WebAPI based project the flags --no-html and --no-webpack just means that we don’t want to our project comes up with webpack or any template engine, by default Phoenix will configure with these features if you don’t specify any of those flags. If you wish, visit this <a href="https://hexdocs.pm/phoenix/phoenix_mix_tasks.html#content">guide</a> to read about all available commands that Phoenix CLI comes up.</p><p>After the project is created, the Phoenix CLI will prompt you if the dependencies should be fetched and installed automatically in case you prefer not to it’s also possible do it later. Running the following command.</p><pre>mix deps.get &amp;&amp; mix deps.compile</pre><h3>My Hero Academy API</h3><p>Well, i am an bigger fan of Anime and you probably already have seen the Pokemon API (Poke API), for those who don’t seen and have curiosity it can be found <a href="https://pokeapi.co/">here</a>. So in this guide, i am going to make a clone, but following with <strong>Boku No Hero </strong>Anime Theme instead of Pokemon.</p><h3>Folder Structure From Project</h3><p>After the project have been created and all dependencies installed. Phoenix CLI give us the following folder structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*YhU9vORF5jHrvtJR_twEGQ.png" /><figcaption>project structure folder generated from phoenix framework</figcaption></figure><p>The<strong> config</strong> folder is where all the configuration stuff related to project is placed like database credentials or any sort of configuration required for a third-party library for instance.</p><p>The <strong>lib</strong> folder is where all source-code like <strong>Controller</strong>, <strong>Repository</strong>, <strong>Schema</strong> (<strong>Model</strong>) and <strong>Views </strong>lives. Notice that some folders have a prefix with <strong>_web </strong>on front of it<strong>, </strong>Phoenix use this to separate the domain logic from the application controller part.</p><p>And finally the <strong>test</strong> folder, where all the tests related from the application is placed.</p><h3>The First Route and Controller</h3><p>To create our first <strong>Route</strong> and <strong>Controller</strong>, Phoenix CLI provide to us a command that generate the <strong>Schema</strong>, <strong>Migration, Repository</strong>, <strong>Controller</strong> and <strong>View</strong>. The command is <strong>phx.gen.json </strong>also<strong> </strong>there is <strong>phx.gen.html</strong>, the main difference between those two, is that the first one use <strong>JSON</strong> to render and the second use the template engine built-in into <strong>Phoenix Framework. </strong>The syntax can be written as the following command below:</p><pre>mix phx.gen.json ContextName schema_name tb_name [ attr_name:type ]</pre><p><strong>Context </strong>in phoenix means a group of related functionality, to understand better this concept, you can read this guide <a href="https://hexdocs.pm/phoenix/contexts.html">here</a>.</p><p>The<strong> </strong>entity will contain the following attributes <strong>first_name</strong>, <strong>last_name</strong> and <strong>nick_name </strong>with type<strong> string. </strong>To create the resources for that you will just need to type the following command inside of the <strong>Phoenix Application</strong>.</p><pre>mix phx.gen.json Heros Hero tb_heros first_name:string last_name:string nick_name:string</pre><h3>Registering our routes</h3><p>Our route is still missing one thing which is, register the route itself. To do that, you can take a look at <strong>router.ex</strong> file inside of <strong>lib/project-name_web folder. </strong>At your <strong>router.ex, </strong>you should include the following line:</p><pre>resources “/heros”, HeroController, except: [:new, :edit]</pre><p>This way Phoenix will be able to know what handler call when you hit the /<strong>heros</strong> endpoint and finally your controller should seems similar to mine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UNAirtDLXFfX8kUtbs263Q.png" /><figcaption>Router file example</figcaption></figure><p>Inside of <strong>Controller </strong>there is a naming convention for the handler functions, but of course it’s not required one, which is basically.</p><blockquote><strong>GET</strong> :index - ideally for list all resources. <br><strong>POST</strong> :create - ideally for create an resource<br><strong>GET</strong> :show - ideally to get an resource by given an id<br><strong>PUT</strong> :update - ideally to update an resource by a given an id<br><strong>DELETE</strong> :delete - ideally to deletes an resource by given an id<br><strong>GET</strong> :new - ideally to show a form to create an resource<br><strong>GET</strong> :edit - ideally to show a form to edit an given resource</blockquote><p>The “<strong>resources</strong>” is a special function that Phoenix allow us to use, as first argument is the path, second argument is <strong>Controller</strong> itself and finally on third you got some optional parameters, on our example, <strong>except</strong> means that we don’t want to some special methods, in the case the <strong>new</strong> and <strong>edit </strong>one<strong>.<br></strong>If you wish have more a grain control over your routes, it’s possible register each one separately. You can read more about <a href="https://hexdocs.pm/phoenix/Phoenix.Router.html">here</a>.</p><p>After you’ve registered that route, it’s possible to check it, to do that just type the following command in the terminal inside of Phoenix application folder.</p><pre>mix phx.routes</pre><h3>Migrations</h3><p>There’s still one thing that we need to do before we can start the server and hit that endpoint, which is create the database and run the migrations. Phoenix uses under-hood <strong>Ecto</strong> to deal with all the SQL part. <strong>Ecto</strong> provides to us a Command Line Interface <strong>(CLI)</strong> to interact with database, the commands you’ll need to know are:</p><blockquote><strong>create</strong> - used to create the database if doesn’t already exists.<br><strong>drop</strong> - used to drop the database.<br><strong>setup</strong>- used to create the database and run all the migrations needed.<br><strong>migrations</strong> - list all the migrations that already have been performed.<br><strong>migrate</strong> - used to setup only the migrations that are needed.</blockquote><p>So, to setup the database and all migrations, you will just need to run the following command in terminal inside of the Phoenix application folder:</p><pre>mix ecto.setup</pre><h3>Running the Server</h3><p>Finally to start the server it’s pretty simple, you will just need to run the following command inside of the Phoenix application:</p><pre>docker-compose up -d &amp;&amp; mix phx.server</pre><p>By default according with the environment Phoenix will start the server at port <strong>4000 </strong>in development mode and in production will be at port <strong>80. </strong>It’s possible to change it through <strong>port</strong> configuration option. Also it’s possible make others configurations such as database environment, <strong>HTTPS</strong> Support or<strong> JWT</strong> Secret Keys for instance.</p><p>Notice that at <strong>config</strong> folder there are multiple files, one for each environment the explanation it’s pretty simple and straightforward:</p><blockquote><strong>dev.exs </strong>- the development configuration file<br><strong>prod.exs </strong>- the production configuration file<br><strong>test.exs </strong>- the testing configuration file</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uW8qqJBMdoFXaf4VEgFYSA.png" /><figcaption>Configuration file example</figcaption></figure><h3>Requests and Response Example</h3><p>After the server it’s up and running we can make a request example to test it. The following image corresponds to a <strong>Request </strong>and<strong> Response </strong>respectively<strong> </strong>with purpose of creates an hero resource.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBLgjlViMRj7OseWNBEUYQ.png" /><figcaption>Request and Response Example</figcaption></figure><h3>Conclusions</h3><p>The main goal of this article was to demonstrate the very basics of using Phoenix Framework. There are still a bunch of topics to cover such as how to handle with authentication, associations, paginate the results for instance. I intend to cover these topics later in a separately article. If you wants to check the source code directly from repository, you can click <a href="https://github.com/augusto1997/my-hero-academy-api">here</a>.</p><p>If you’ve any doubt or trouble, please feel free to leave a comment or reach me out through a private message, i will do my best to answer you all.</p><p><a href="https://github.com/augusto1997/my-hero-academy-api">augusto1997/my-hero-academy-api</a></p><h3>References</h3><p>[1]: Installing Elixir with Mix Tool. <a href="https://elixir-lang.org/install.html">https://elixir-lang.org/install.html</a><br>[2]: PokeApi Project. <a href="https://pokeapi.co/">https://pokeapi.co/</a><br>[3]: Phoenix Router Documentation. <a href="https://hexdocs.pm/phoenix/Phoenix.Router.html">https://hexdocs.pm/phoenix/Phoenix.Router.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa3a0a23b574" width="1" height="1" alt=""><hr><p><a href="https://medium.com/red-ventures-br-tech/elixir-building-a-rest-api-using-phoenix-framework-part-i-fa3a0a23b574">Elixir: Building a REST API using Phoenix Framework Part I</a> was originally published in <a href="https://medium.com/red-ventures-br-tech">Red Ventures Brasil - Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>