REST API
Petal Pro comes with a pre-built REST API. Though it is limited in scope, the API has been created so that it is easy to extend
The REST API is a sample API that has been created for user management. For example, you could use the API with a mobile application to register a user, sign them in and update their profile.
Though the API is provided as an example, it can be easily extended or updated. The key to this extensibility is the selection of technologies:
- The Phoenix Framework provides first-class support for creating APIs
-
phx.gen.authis used as a basis for API Authentication - Open API support via Open API Spex
Taking Advantage of the Phoenix Framework
With the Phoenix Framework, you can use the following command to generate a JSON API:
mixphx.gen.jsonUrlsUrlurlslink:stringtitle:string
This creates a set of files that take care of the database schema, data access control, the JSON for the output and testing. All you are required to do is add the suggested route to the /api scope. This provides a complete/comprehensive starting point for your own work.
ℹ️ Information: The Phoenix generator will put your controller under:
``elixir
/lib/petal_pro_web/controllers
`
This isn't a problem, but you might like to move it to where the other API controllers are:
`elixir
/lib/petal_pro_api/controllers
``
The same is true for the test that's generated. See [Directory Structure](/petal-pro/fundamentals/rest-api#directory-structure) (below) for more details