Post

REST API Naming Conventions: Best Practices and Guidelines

REST API Naming Conventions: Best Practices and Guidelines

Naming your API properly is the first step in designing a good API. A well-structured API name follows a convention that makes it intuitive, readable, and maintainable. A meaningful API endpoint should follow some simple yet essential guidelines and rules.

In this article, you’ll learn the best practices for naming API endpoints and understand the difference between good and bad API routes. By following these conventions, you can create REST APIs that are clear, scalable, and easy to use.


Rule 01: Use Lowercase, Hyphens, and Full Words

Key Points:

  • Always use lowercase in API endpoints.
  • Use hyphens (-) instead of underscores (_) or spaces to separate words.
  • Avoid abbreviations—use full, meaningful words instead.
  • If an API accepts a variable, wrap it inside curly braces {} and use camelCase.

Examples

URIStatusWhy?
/products✅ GoodUses plural and lowercase.
/products/42/product-details✅ GoodUses lowercase and hyphen.
/orders/78/shipping-address/home✅ GoodUses lowercase and follows a hierarchy.
/employees/{employeeId}✅ GoodVariable in camelCase.
/Products❌ BadUses title case.
/activeUsers❌ BadUses camelCase instead of hyphens.
/CategoryList❌ BadUses PascalCase.
/orders/78/shipping-addr❌ BadUses abbreviation.
/products/42/product_description❌ BadUses underscores.
/employees/{employee-id}❌ BadUses hyphen inside variable instead of camelCase.

Rule 02: Use a Forward Slash (/) to Indicate Hierarchy

The forward slash / should be used to indicate a hierarchical relationship between resources.

Example Hierarchies

E-commerce API

A store has products, and customers place orders. Each order has a shipping address.

URIStatus
/store/products/{productId}/reviews✅ Good
/store/orders/{orderId}✅ Good
/store/orders/{orderId}/shipping-address✅ Good

Education API

A university has students enrolled in courses, and each course has an instructor.

URIStatus
/university/students/{studentId}/courses✅ Good
/university/courses/{courseId}/instructor✅ Good

Rule 03: Use Nouns, Not Verbs

RESTful APIs treat resources as nouns, not actions. The HTTP methods (GET, POST, PUT, DELETE) define the actions.

Examples

URIExpectsStatusWhy?
/booksCollection✅ GoodUses a noun, not a verb.
/users/{userId}/profileSingle user✅ GoodProper naming convention.
/bookCollection❌ BadUses singular for collection.
/fetchBookSingle resource❌ BadUses a verb.
/retrieveUser/{userId}Single user❌ BadUses a verb.

Rule 04: Avoid Special Characters

Avoid using special characters (|, ^, @, #, !, etc.) in API endpoints. If your API needs to handle multiple values, use commas for separation.

Examples

URIStatusWhy?
/students/101,102,103/grades✅ GoodUses a comma for separation.
/students/101\|102\|103/grades❌ BadUses a special character |.
/products/32/price^discount❌ BadUses a special character ^.

Rule 05: Avoid File Extensions in URIs

APIs should not include file extensions like .json or .xml. Instead, clients should request different formats using a query parameter.

Examples

URIStatusWhy?
/music/songs/{songId}?format=json✅ GoodUses a query string.
/music/songs/{songId}?format=xml✅ GoodUses a query string.
/music/songs/{songId}.json❌ BadUses file extension.
/music/songs/{songId}.xml❌ BadUses file extension.

For static files like JavaScript and CSS, use clear endpoints and query strings instead of file extensions.

URIStatusWhy?
/assets/css/bootstrap/5.3/min✅ GoodNo file extension.
/assets/css/bootstrap/5.3/source✅ GoodNo file extension.
/assets/css/bootstrap/5.3/?format=min✅ GoodUses a query string.
/assets/css/bootstrap/5.3/?format=source✅ GoodUses a query string.
/styles/main.css❌ BadUses a file extension.

Rule 06: Use Query Parameters for Filtering

Use query parameters (?key=value) for filtering, pagination, and optional details instead of embedding them in the URI.

Examples

URIStatusWhy?
/events/{eventId}/attendees✅ GoodClear hierarchy.
/events/{eventId}/attendees?status=VIP✅ GoodUses a query parameter.
/posts?limit=5&page=3✅ GoodProper pagination.
/events/{eventId}/attendees/VIP❌ BadDoesn’t use a query string.
/posts/page/3/limit/5❌ BadOvercomplicates the URL.

Rule 07: No Trailing Slash

Avoid trailing slashes (/) at the end of API endpoints. They can lead to inconsistencies and unnecessary redirects.

Examples

URIStatusWhy?
/users/{userId}✅ GoodNo trailing slash.
/blog/{postId}/comments✅ GoodNo trailing slash.
/users/{userId}/❌ BadEnds with a trailing slash.
/blog/{postId}/comments/❌ BadEnds with a trailing slash.

Conclusion

Following these REST API naming conventions ensures that your API is consistent, readable, and easy to maintain. A well-structured API reduces confusion, improves usability, and enhances developer experience.

This post is licensed under CC BY 4.0 by the author.