In REST APIs, we often need to get a single resource. In this article, we explore how to structure those GET requests properly.
π§ββοΈ Get a Single Account by ID
To get one specific account, use:
/accounts/{id}
Example:
/accounts/adoe
This will return the account with ID adoe
.
You can apply the same idea to other resources, like:
/applications/872233
π§ Get Resource(s) by Field Value
Sometimes you want to retrieve a resource or a small list of resources based on a specific field, like email or region. Instead of creating multiple endpoints, use query parameters.
β Good Practice: use query parameters
/[email protected]
This means: Get all accounts where email equals [email protected]
Another example:
/accounts?region=EMEA&role=admin
This means: Get accounts in EMEA region with admin role
You can combine multiple query parameters β the order doesnβt matter.
β Bad Practice: encoding fields in the path
Avoid things like:
/accounts/email/[email protected]
or
/accounts/region/EMEA
It breaks RESTful conventions and is harder to maintain.
β οΈ Special Characters in IDs
Sometimes IDs or field values contain characters like @
, /
, or &
. Always URL-encode these to prevent errors. For example:
/accounts?email=adoe%40company.com
β Summary
To get a single resource or filter resources by field:
- Use
/resource/{id}
to fetch by unique ID - Use
/resource?field=value
for flexible searches - Never include filter values in the URL path
- Always encode special characters properly
- Stick to
GET
for safe, idempotent fetch operations
π Enjoyed this deep dive?
This is just a taste of what's coming! The platform I'm building, IntegrationTrails.io, is launching soon!
It's a place where you'll go beyond reading to truly boost your skills with practical guides, hands-on projects, and step-by-step learning experiences.
π Be the first to know → Join the waitlist!