r/PHPhelp • u/GuybrushThreepywood • Sep 26 '25
How to structure my api endpoints?
I've not created an API before and I would like to get it right from the outset. I'm planning to use Laravel to create api, however I am a bit unsure on how to structure the endpoints.
I have clients who are using my systems. E.g.:
Client A:
System id 001 in London
System id 002 in Manchester
Client B:
System id 003 in Liverpool
The system has users, rooms and schedules.
I have so far considered these endpoints (I will use JWT for auth)
Systems
GET: /v1/systems Get all systems for user
GET: /v1/systems/001 Get system 001 info
Users
GET: /v1/systems/001/users Get all users for system 001
POST: /v1/systems/001/user Create a new user on system 001
Rooms
GET: /v1/systems/001/rooms Get all rooms on system 001
GET: /v1/systems/001/room/25 Get room 25 info from system 001
POST: /v1/systems/001/room Create a new room on system 001Alternatively, I have considered:
Systems
GET: /v1/systems Get all systems for user
GET: /v1/systems/001 Get system 001 info
Users
GET: /v1/users/001 Get all users for system 001
POST: /v1/user/001 Create a new user on system 001
Rooms
GET: /v1/rooms/001 Get all rooms on system 001
GET: /v1/room/001/25 Get room 25 info from system 001
POST: /v1/room/001 Create a new room on system 001And also:
Systems
GET: /v1/systems Get all systems for user
GET: /v1/systems/001 Get system 001 info
Users
GET: /v1/users?system=001 Get all users on system 001
POST /v1/user?system=001 Create a new user on system 001Which approach should I use? Any tips & advice welcome
4
Upvotes
8
u/obstreperous_troll Sep 26 '25
The first, no question. The second one makes it impossible to address users or rooms individually, since you've made all numbers refer to systems. I'd also suggest using UUIDs or ULIDs for everything so you can address a room or user directly without having to know the system first. Frees you up to use friendlier "slug" names in the hierarchical paths should you choose.
4
u/rmb32 Sep 26 '25
If rooms and users have their own unique IDs then I would address them alone, not by their system:
GET /v1/rooms/123 Get room 123 info
POST /users Create a user and post the system ID as form data or JSON
3
u/Alternative-Neck-194 Sep 26 '25
You don’t have to pick one style exclusively. Nested routes like
/v1/systems/001/usersare nice, but in my experience, you’ll soon need more flexible filtering. Like users from multiple systems and more filter for example:/v1/users?system.in=1,2&room.info=foo&user.email.startsWith=barA good approach is to support all: let
/v1/systems/001/usersinternally resolve to/v1/users?system=001. This gives you clean URLs for common cases and query-based filtering for more advanced needs.2
u/iamprogrammerlk_ Sep 27 '25
Getting all the records (user/room/system)
Getting a single record (user/room/system)
Getting a filtered record (user/room/system)
Create/update/delete (POST/PUT or PATCH/DELETE) work on the same endpoint to create, update, or delete the data.
Please do not use the plural (users/rooms/systems) in your endpoints; only use the singular (user/room/system) words.
Learn more about the API design https://cloud.google.com/apis/design
1
u/MartinMystikJonas Sep 27 '25
I would consider making system part od domain if systems are fully independent.
system001.api.something.com system002.api.something.com
This allows you to use separate DNS for separate systems instead of routing everything throught single endpoint.