Skip to navigation
Hello World Phoenix framework setup guide
07.04.26
"Hello World" Phoenix framework setup guide, focusing on clarity and ease of understanding: # Hello World in Phoenix (API Only) This guide walks you through creating a simple "Hello World" API endpoint using the Phoenix framework. We'll build a basic API that returns a JSON response. **Prerequisites:** * Elixir and Erlang installed (check [https://www.phoenixframework.org/docs/installation](https://www.phoenixframework.org/docs/installation) for installation instructions) **Steps:** 1. **Create a New Phoenix Application:** Open your terminal and run the following command to generate a new Phoenix project named "hello". We'll use SQLite3 for the database and skip generating HTML views and assets (since we're building an API): ```bash mix phx.new hello --database sqlite3 --no-html --no-assets ``` When prompted to install dependencies, type `no` and press Enter. We'll install them manually in the next step. 2. **Install Dependencies:** Navigate into the newly created project directory: ```bash cd hello ``` Now, install all the project's dependencies: ```bash mix deps.get ``` 3. **Create the Database:** Create the SQLite database for your application: ```bash mix ecto.create ``` 4. **Create a Controller:** Create a new file at `lib/hello_web/controllers/page_controller.ex` with the following content. This controller will handle the request and return a JSON response: ```elixir defmodule HelloWeb.PageController do use HelloWeb, :controller def home(conn, _params) do value = "hello" json(conn, %{id: value}) end end ``` **Explanation:** * `use HelloWeb, :controller`: This imports the necessary functions and macros for defining a controller in your Phoenix application. * `def home(conn, _params) do`: This defines a function named `home` that takes two arguments: `conn` (the connection struct, representing the incoming request) and `_params` (any parameters passed in the request, which we're ignoring here). * `value = "hello"`: This assigns the string "hello" to the variable `value`. * `json(conn, %{id: value})`: This is the key part. It uses the `json` function (provided by Phoenix) to send a JSON response. The response will be a JSON object with a single key, `"id"`, and the value `"hello"`. 5. **Add a Route:** Open the `lib/hello_web/router.ex` file and add the following route inside the `scope "/", HelloWeb do` block: ```elixir scope "/", HelloWeb do pipe_through :api get "/", PageController, :home end ``` **Explanation:** * `scope "/", HelloWeb do`: This defines a scope for routes under the root path (`/`) and associates them with your `HelloWeb` application. * `pipe_through :api`: This specifies that the `:api` pipeline should be used for these routes. The `:api` pipeline is defined in the same file and typically includes middleware for handling API requests (e.g., parsing JSON requests). * `get "/", PageController, :home`: This defines a route that handles GET requests to the root path (`/`). It specifies that the `home` function in the `HelloWeb.PageController` module should handle the request. 6. **Start the Phoenix Server:** Run the following command to start the Phoenix server: ```bash mix phx.server ``` This will start the server and listen for incoming requests on `http://localhost:4000`. 7. **Test the API:** Open your web browser or use a tool like `curl` or `Postman` to send a GET request to `http://localhost:4000/`. You should see the following JSON response: ```json {"id": "hello"} ``` Congratulations! You've successfully created a simple "Hello World" API endpoint using the Phoenix framework.
https://hexdocs.pm/phoenix/controllers.html
Reply
Anonymous
Information Epoch 1777128194
Use lower case and keep it short.
Home
Notebook
Contact us