Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

a.verrecchia's avatar

Using UUIDs in API Routes - Balancing Privacy and Performance

How should public UUIDs be used in API routes? Should I use UUIDs in the routes and then convert them to internal IDs in case of relationships to avoid losing performance, while if I only need to retrieve a record I use the WHERE clause with the UUID since the delay compared to the ID is negligible. Now, does it make sense to also use UUIDs in routes where the level of privacy is lower? For example:

{{baseURL}}/businesses/bc75a13c-2c82-47fc-90fc-7df035e18c64/reviews/6321e43d-985b-422e-8644-e6082adbd90c

In this case, businesses are still public, data that anyone can see. The same goes for reviews. Is it preferable to use this method or use a classic auto-incremented ID?

{{baseURL}}/businesses/1/reviews/1

In this other way, I still have doubts. Let's say it's very "playable", so just put 2 and find the second review, etc.

0 likes
1 reply
LaryAI's avatar
Level 58

It ultimately depends on your specific use case and the level of privacy you want to maintain. If you are okay with exposing UUIDs in your API routes, then you can use them without any issues. However, if you want to maintain a higher level of privacy, you can use auto-incremented IDs instead.

If you decide to use UUIDs, you can convert them to internal IDs for relationships to improve performance. Here's an example of how you can do this in Laravel:

// In your model
public function getRouteKeyName()
{
    return 'uuid';
}

// In your controller
public function show(Business $business, Review $review)
{
    // Convert UUIDs to internal IDs
    $businessId = $business->id;
    $reviewId = $review->id;

    // Retrieve the record using internal IDs
    $review = Review::where('business_id', $businessId)
                    ->where('id', $reviewId)
                    ->first();

    // Return the response
    return response()->json($review);
}

This way, you can use UUIDs in your routes while still maintaining good performance.

If you decide to use auto-incremented IDs, you can still use them in your routes without any issues. However, you may want to consider adding some form of authentication or authorization to prevent unauthorized access to your data.

// In your controller
public function show(Business $business, Review $review)
{
    // Retrieve the record using auto-incremented IDs
    $review = Review::where('business_id', $business->id)
                    ->where('id', $review->id)
                    ->first();

    // Return the response
    return response()->json($review);
}

In summary, both UUIDs and auto-incremented IDs can be used in your API routes. It's up to you to decide which one to use based on your specific use case and the level of privacy you want to maintain.

Please or to participate in this conversation.