AshJsonAPI - How to pass a tenant?

I am running curl commands against my AshJsonAPI endpoint and, as expected, I receive a 'tenant must be set' error as I am not passing the tenant.
curl -XPOST -H 'Content-Type: application/vnd.api+json' localhost:4000/api/json/items -d '{"data": {"type": "item", "attributes": {"name": "test", "provider_id": "01985d4d-f0c4-73b5-8919-29946afeb08c", "deploys": []}}}'
curl -XPOST -H 'Content-Type: application/vnd.api+json' localhost:4000/api/json/items -d '{"data": {"type": "item", "attributes": {"name": "test", "provider_id": "01985d4d-f0c4-73b5-8919-29946afeb08c", "deploys": []}}}'
However I can't seem to pass the tenant outside or inside of the "data" field. What is the structure to pass the tenant alongside the data?
2 Replies
ZachDaniel
ZachDaniel3mo ago
The tenant is typically set via some other context, like a subdomain, or a header, and handled in your plug pipeline I don't think we've had a case to allow specifying tenant in the actual body
Malian
Malian17h ago
For those who face the same issue, we chose to pass the X-Org-Id header in API requests and use the Ash.PlugHelpers.set_tenant function in a custom plug that retrieve the value from the header. The problem with this method is that the SwaggerUI interface no longer works because it is not possible to pass this specific header for every request. Therefore, we used the option modify_open_api: {__MODULE__, :modify_open_api, []} to modify the open api file. For each route/path where it was relevant, we added a parameter:
%OpenApiSpex.Parameter{
name: “X-Org-Id”,
in: “header”,
description: “Org ID for organization authentication”,
required: true
}
%OpenApiSpex.Parameter{
name: “X-Org-Id”,
in: “header”,
description: “Org ID for organization authentication”,
required: true
}

Did you find this page helpful?