Search code examples
apirestapi-design

Advice on designing a RESTful API for a simple ticket system


Lately I have started learning Go. In order to practice my newly acquired skills, I decided to start a small side project. In this project I want to design and build a RESTful API for a ticket system. Since I'm still quite new to this topic, I'm looking for advice on how to do it right. The system should be able to create new events that can be booked by registered and unregistered users. Therefore I thought of the following structure for my API:

login:
    /api/login/
    method POST

logout:
    /api/logout/
    method POST

users:
    /api/users/create (create user)
    method POST

    /api/users/{id} (get, update and delete user)
    method GET, PUT, DELETE 

    /api/users (get all user)
    method GET

events:
    /api/events/create (create new event)
    method POST

    /api/events/{id} (get, update and delete event)
    method GET, PUT, DELETE 

    /api/events (get all events)
    method GET

bookings:
    /api/bookings/create (create new booking for a certain event)
    method POST

    /api/bookings/{id} (get, update and delete booking)
    method GET, PUT, DELETE 

    /api/bookings (get all bookings for a certain event)
    method GET

How can I improve this structure? What would you do differently?


Solution

  • Is there a specific reason why you integrated /create into your creation URLs?

    A resource is typically created by POSTing to the root resource. For example:

    users:
        /api/users (create user)   <-- change here
        method POST
    
        /api/users/{id} (get, update and delete user)
        method GET, PUT, DELETE 
    
        /api/users (get all user)
        method GET
    
    same for others ...
    

    Don't forget to respond with HTTP 201 and a Location-Header after a successful resource creation.

    How do bookings and events relate to each other? If a booking always belongs to a specific event, you can model bookings as a sub-resource of event.

    For example:

    POST /api/events/<id>/bookings      <-- Create a booking for a specific event
    GET /api/events/<id>/bookings       <-- Get all bookings for an event
    GET /api/events/<id>/bookings/<id>  <-- Get specific booking
    PUT /api/events/<id>/bookings/<id>  <-- Update booking