Building our API

If we try to run API requests against our application right now using a tool like a cURL or a Postman we will (rightfully) get an error message since we have no real framework in place to serve API requests. The good news is that Phoenix makes everything incredibly simple to build a new API with very, very little dependency on extra libraries to get everything put together.

In fact, building an API in Phoenix is such a simple endeavor that it has completely replaced my usual defaults of other languages and tools! Everything is given to you in some way or another without requiring a lot of extra work or configuration, and having an API live seamlessly side-by-side with the rest of your application is a breeze!

If we start off by making a request to our non-existing API routes, we will (as expected) get an error message. For purposes of this chapter, I'll be using Postman to make and showcase the API functionality, but you could easily use another tool or just plain old cURL!

The following screenshot is an example of using Postman to send our API requests. You can see the URL we're hitting, as well as any configuration and headers we may be set as well, so this tool makes it especially easy to follow along:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset