A Friendly Guide to RESTful APIs: Core Concepts and Top Practices

Introduction

Hey there! If you’re a software developer or an aspiring one, you’ve probably heard the term “RESTful API” quite a lot. APIs, or Application Programming Interfaces, play an essential role in connecting applications and services. RESTful APIs are particularly popular, thanks to their simplicity and versatility. In this article, we’ll go through the core concepts of RESTful APIs and some best practices to give you a solid understanding of what they’re all about.

Understanding REST

REST, short for Representational State Transfer, is an architectural style used for designing networked applications. Roy Fielding introduced REST in his 2000 doctoral dissertation, and it has since become a favorite among developers for building scalable and maintainable web services. REST is built on the principles of the World Wide Web, providing a set of guidelines to ensure consistency and predictability.

The main principles of REST include:

  1. Stateless: Each client request should contain all the necessary information for the server to process it. The server doesn’t store any client state information between requests.
  2. Client-Server: The client and server are separate entities that communicate over a network, allowing each side to evolve independently.
  3. Cacheable: Clients can cache server responses, which can lead to better performance and reduced server load.
  4. Layered System: RESTful architectures can have multiple layers, with each layer providing specific functionality. This modular approach helps maintain a separation of concerns.
  5. Code on Demand (optional): Servers can provide executable code (e.g., JavaScript) to clients, extending their functionality when needed.
  6. Uniform Interface: RESTful APIs follow a set of conventions for resources, methods, and messages, making them easy to understand and use.

Key Components of RESTful APIs

To build a RESTful API, you’ll need to work with several components:

A. Resources: In a RESTful API, resources represent the entities or objects the API exposes. Resources can be anything, from users and products to more abstract concepts like settings or preferences.

B. URIs (Uniform Resource Identifiers): Each resource in a RESTful API is uniquely identified by a URI, a human-readable way to reference the resource. URIs typically follow a hierarchical structure, such as “/users/{user_id}/orders/{order_id}”.

C. HTTP methods: RESTful APIs use standard HTTP methods to perform operations on resources. The most common methods are GET (retrieve a resource), POST (create a new resource), PUT (update an existing resource), PATCH (partially update a resource), and DELETE (delete a resource).

D. HTTP status codes: When a server responds to an HTTP request, it includes a status code indicating the outcome of the request. Common status codes include 200 OK (success), 201 Created (resource created), 400 Bad Request (invalid request), 404 Not Found (resource not found), and 500 Internal Server Error (server error).

E. Data formats: RESTful APIs typically use JSON (JavaScript Object Notation) or XML (eXtensible Markup Language) to represent resources in a format that’s both human-readable and machine-parsable.

Top Practices for Crafting RESTful APIs

To create an efficient and easy-to-maintain RESTful API, keep these best practices in mind:

A. Stick to consistent naming conventions: Adopt clear, consistent naming for your resources and URI paths. Use plural nouns for collections (e.g., “/users”) and singular nouns for individual resources (e.g., “/user/{user_id}”). Avoid using underscores and capitalize only the first letter of each word in multi-word path segments (e.g., “/userSettings”).

B. Embrace versioning: As your API evolves, you may need to make changes that are not backward-compatible. Incorporate versioning into your API by including a version number in the URI (e.g., “/v1/users”) or using custom headers. This approach ensures existing clients are not impacted when you release new API features or updates.

C. Leverage proper HTTP methods and status codes: Use the appropriate HTTP method for each operation and return meaningful status codes in your responses. This makes your API more self-descriptive and helps developers understand the expected behavior.

D. Implement pagination and filtering: For endpoints that return large collections of resources, provide pagination and filtering options to improve performance and usability. Include query parameters in the URI to control the page size, sorting, and filtering criteria (e.g., “/users?limit=10&page=2&sort=lastName,asc”).

E. Prioritize security and authentication: Protect your API by implementing proper authentication and authorization mechanisms, such as OAuth, JSON Web Tokens (JWT), or API keys. Also, ensure that sensitive data is encrypted, both in transit and at rest.

F. Provide comprehensive documentation: Clear, concise documentation is essential for an effective API. Include detailed descriptions of resources, methods, request and response formats, and examples of usage. Tools like Swagger or OpenAPI can help generate interactive documentation that developers can explore and test.

Conclusion

RESTful APIs are a powerful tool for modern software development, allowing seamless integration of features and services across applications. By understanding and applying the principles, best practices, and real-world examples shared in this article, you’ll be well-prepared to design, implement, and maintain effective RESTful APIs for your projects.

As you dive deeper into the world of RESTful APIs, remember to iterate and refine your API based on user feedback and evolving

Beyond Computer Science Degree

graduates

Hi there!

If it is not obvious by now, I am a Software Engineer (SWE) with almost 6 years of experiences in this field. Not only I wrote this post just to brag about my experience, I would also like to share with you what kind of skills you strive to work for if you are an aspire software developer and still studying in your university, or you are planning to transition your career as an SWE.

But wait

Before I share with you the skills, I want to rant for a brief moment. Throughout my career, I had the opportunity to interview almost 10 candidates who were applying as an entry level software engineer in my company. What I notice is that, a lot of them are not equipped with the basic skills that would enable them to work as an SWE. Whenever I asked them a certain questions, for example like, “what tools we can use to manage software versions?”, most of the time, I hear the same answers, “I don’t know, we were not taught such tools in school.” The answer is Git by the way.

I’m not blaming the candidates for not knowing the answers, I can relate their confusions for not knowing what skills are expected since schools don’t really cover the actual skills that you would actually use in our day to day job as an SWE. What schools actually prepare us is the foundation for every possible IT jobs which their students might have taken after they gradudated. This is why we were taught so many theories and concepts in schools, but only few of them are actually useful for the job.

I’m not saying schools are doing a terrible job to prepare these students, teaching everything what each of the students might need to know for their future job is an impossible task. Some of the students don’t even know what they want to become after they graduated.

Who is this article for?

This article is written for those students who absolutely know that they want to become an SWE. It’s crystal clear for them that they want to work in a job where they are going to build products by programming. If this is you, you’ve come to the right place, and I shall explain to you all the skills that you can accumulate slowly in your time as a Computer Science students. Do note that, you are not expected to know all of them or become a master in any of them. By demonstrating awareness of the existence of some of these tools, knowing what they are used for, and had short experience in using them is hundred times better in the eye of potential employers than getting a first class but not knowing anything.

Skills you might need to know

Programming skill and understanding its foundation

Duh! It’s obvious you need to know how to programme in any programming language that you prefer. You might be surprised the audacity of including this skill in the list, but trust me, my non-scientific estimate is that 90% of Computer Science graduates don’t even know how to programme. A lot of them don’t even know how to connect to the database. If you understand programming’s foundation, know how to build a class (for OOP programming language), know how to call a function from other class, know how to connect to the database and perform DB operation, etc, you are a brilliant candidate. Knowing programming foundations already puts you in the top 20% entry-level candidates.

Version Control using Git

Git is a tool that you can use to version control your project. What does this mean? Let me use an example that hit close too home. Instead of naming your project folder as MyProject_Final, MyProject_FinalV2, MyProjectFinalFinal (you get the point), you can just use git to “store” all the changes you made into your code into a series of node called “commit”, in this way, you can just go back and forth to each of your code changes. You can also use “branch” to safely make any changes that you want to apply to your code, and if you’re not happy with it, you can destroy the branch and go back to the main branch that you are working on. Git is always used in every Software Development team. You can learn this in a day.

GitHub (or other Git services)

Whenever you store your changes as a commit using Git, your code and the changes still reside in your local pc. If you accidentally permantenly deleted your code folder (definitely never happened to me), you lost all your code and wouldn’t be able to retrieve them back. To prevent this from happening, you can use git to “push” your code to GitHub (for free) and have your code hosted in GitHub. The cool thing about hosting your code in GitHub allows you to collaborate with your friend (you guys collaborate right?) on the same code “repo”, perform “push” and “pull” to your synchronise your code with each other. Using this approach, no one would have to do the painstaking task to compile all the codes from each others’ pen drive and ensuring the codes are in harmony one day before the deadline.

If you have a personal project, it’s also a good habit to publish in publicly in GitHub so your future employers can see how “awesome” your code is and gauge your ability as a developer. Whenever I have a task that I want to give to potential employee, I always ask them to publish their codes into GitHub.

Be comfortable with your preferred programming language

There are tons of programming language are available that you can use. However, you need to be at least comfortable in using one or two programming language. Since most of my reader probably coming from Brunei, there are several programming languages that are commonly used in this country. You can’t go wrong with any of these languages:-

  • PHP
  • JavaScript
  • C#
  • Python
  • Java and Swift (for mobile developer)

Above are the languages that I’m 100% sure are widely used in Brunei, though I have heard several companies using these as well:-

  • Ruby
  • Golang
  • Kotlin

My suggestion: learn PHP and JavaScript if you want to become web developer.

Be familiar with frameworks

Whenever a new web or mobile app project is started, rarely it is developed from scratch. Most companies rely on third-party frameworks as the foundation for their projects as this will reduce development time and make the software robust, as these frameworks are battle tested and used by thousands of companies as well. Developers are not keen to build certain core functionalities e.g., routing, csrf protection, authentication, authorization, etc., as we are more interested with building business related features.

Some frameworks that you can learn:

Backend Framework

  • PHP: Laravel, Symfony, CodeIgniter, Phalcon, CakePHP
  • C#: ASP.NET (Blazor, MVC, Razor Page)
  • Java: Spring
  • Python: Django, Flask, FastAPI
  • Javascript: ExpressJS, NestJS, AdonisJS

Frontend Framework

  • ReactJS
  • VueJS
  • Angular
  • JQuery

CSS (Styling) Libraries

  • TailwindCSS
  • Bootstrap
  • BulmaCSS

Blogging tool

  • WordPress & WooCommerce

Familiariaty with HTML, CSS, and JavaScript

If you want to become a web developer that deals with the frontend – the part of the website that you see – you need to at least understand basic HTML, CSS, and JavaScript. Most universities cover these technologies so I won’t bother dive deeper into this topic.

Knowing what databases are

Another important technologies that you need to be aware of is databases as it is where applications data will be stored. Generally, there are two types of major database: relational database and document database, though there are more kinds of databases that are used for other niche cases. Most of the time, school will only teach you relational database, for most use cases, it’s enough. Still you need to know the what are the differences between relational database and document database, and also what are the example of these databases.

Relational database is a database where records are stored in tables as row of records and the fields are stored in the tables’ columns. Most of the time, you need to first define what tables that you need and what are their columns before you can start storing records into the database.

As for document database, it is more flexible as you can just insert any kinds of records that you want without defining the table structure. In fact, the concept of table is non-existent in document database. Although it is flexible, but from my experience, it can get hairy once your database’s records grow.

Relational Databases

  • MySQL (My favourite)
  • PostgreSQL
  • SQLite
  • MS SQL Server
  • Amazon Aurora DB

Document Databases

  • MongoDB
  • Amazon DocumentDB

HTTP

Most software development projects nowadays involved with communicating data between websites and web services through the Internet. The common communication method used is HTTP. In my opinion this is the most important foundation concept every web developers need to understand.

You really need to understand the concept of GET and POST request. When to use GET request and when to use POST request. Bonus point if you are aware of other types of request as well: DELETE, PUT, and PATCH.

Knowing this concept will enable you to easily understand AJAX and RESTful APIs which are commonly used in Web Dev projects.

Code Deployment

There are hundreds of ways you can deploy your codes depending of your project. For a beginner, I would just suggest you to learn how to deploy basic Frontend application without any backend logic and database to services like Netlify or Vercel. They are the quickest way to publish your web projects to the Internet.

If you are interested in Backend Development, which involved in databases and business logic, you might need to use Cloud Virtual Machines to provision your server and deploy your backend application to the server. You can get cheap Cloud VMs using DigitalOcean, Linode, or Vultr.

Conclusion

There are lots of things for you to cover here! If you are overwhelmed by this information, don’t worry! Learning to become a software engineer is not a race. It is a slow journey that you need to spend a good amount of time to become at least decent. Remember, you don’t have to be excellent in all of these skills and technologies that I have mentioned. As long as you display a basic understanding of these concept to the person who will be interviewing you, you are already highly considered to be part of the team.

Still confuse?

I know it’s hard to figure out what is the first step that you need to undertake to become proficient software engineer. If you need pointers, here is my opinionated suggestion that you can do:-

Build a Todo App Project using Laravel (PHP) as the backend with MySQL as the database and Bootstrap as the CSS library. Full CRUD (Create, Read, Update, Delete) feature.

Of course you can swap any backend frameworks, programming language, CSS library that you want. Most of the time, this can be learned and completed in less than 3 months.