Sakthibalan

Sakthibalan

How do create custom api to get a user by their :first_name and :date_of_birth?

read :get_user do
  argument :first_name, :string do
    allow_nil? false
  end

  argument :date_of_birth, :date do
    allow_nil? false
  end
end

Showing Posts 1 to 6

shankardevy

shankardevy

There isn’t much information in your post so I’m assuming you have setup the AshJsonAPI as described here: Getting started with AshJsonApi — ash_json_api v1.7.0 . With that assumption, I’m guessing this is what you need on the User resource to get you the API you need.

defmodule Hello.User do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    domain: Hello.Users,
    extensions: [AshJsonApi.Resource]

  postgres do
    table "users"
    repo Hello.Repo
  end

  attributes do
    uuid_primary_key :id

    attribute :first_name, :string, public?: true
    attribute :date_of_birth, :date, public?: true
    .... other attributes
  end

  json_api do
    type "user"

    routes do
      base "/users"

      get :get_user, route: "/find"
    end
  end

  actions do
    read :get_user do
      argument :first_name, :string do
        allow_nil? false
      end

      argument :date_of_birth, :date do
        allow_nil? false
      end

      filter expr(first_name == ^arg(:first_name) && date_of_birth == ^arg(:date_of_birth))
    end
  end

end

Basically, you are missing the filter macro in your read action. In the get route, the first argument :get_user is the read action name defined inside actions and the route option is given to make url as GET /users/find, otherwise the default route for a get path will result in /users/:id where :id is a placeholder and is not relevant in your case.

The API will return all the fields marked as public?: true in the attributes section.

Sakthibalan

Sakthibalan OP

filter expr(first_name == ^arg(:first_name) && date_of_birth == ^arg(:date_of_birth))

** (RuntimeError) Cannot use `&&` without adding the extension `ash-functions` to your repo.

I already tried filter macro, but I got an issue with invalid_filter_value:

filter expr(first_name == ^arg(:first_name) and date_of_birth == ^arg(:date_of_birth))
{
    "errors": [
        {
            "code": "invalid_filter_value",
            "id": "26d912d6-c9d0-4a42-a00a-3683d236df07",
            "status": "400",
            "title": "invalid_filter_value",
            "detail": "Invalid filter value `\"user\"`"
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}
shankardevy

shankardevy

In my Repo module, I have the following:

defmodule Hello.Repo do
  use AshPostgres.Repo,
    otp_app: :hello

  # Installs Postgres extensions that ash commonly uses
  def installed_extensions do
    ["ash-functions"]
  end
end

Are you using Ash 3 or 2? I think this got added recently in Ash 3. Official instructions are here: Get Started With Postgres — ash_postgres v2.10.0

Sakthibalan

Sakthibalan OP

"Raised error: 014f3a37-22d5-4875-8014-2767c1dd5417\n\n** (Ash.Error.Query.InvalidFilterValue) Invalid filter value

zachdaniel

zachdaniel

Creator of Ash

Your uuid has new lines at the end, it’s not a valid uuid

Sakthibalan

Sakthibalan OP

Thank you so much.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews