How to provide meaningful error messages in your API response

Let's learn how to provide meaniful error messages through examples.

Clear and concise messages

Make sure the message is clear and easy to understand for the end user.

{
  "error": "Invalid input: Missing required parameter 'email'."
}

Include error codes

Include error codes in the response to help developers debug the issue.

{
  "error": {
    "code": 400,
    "message": "Bad Request: Invalid input data."
  }
}

Provide suggestion to fix the issue

Sometimes the error message is not enough to fix the issue. In that case, provide a suggestion to help the user fix the issue.

{
  "error": {
    "code": 404,
    "message": "Not Found: The requested resource does not exist. Please check the URL and try again."
  }
}

Make sure to not leak internal errors

Do not leak internal errors to the end user. Instead, log the error and provide a generic error message.

{
  "error": {
    "code": 500,
    "message": "Internal Server Error: Something went wrong. Please try again later."
  }
}

Localization

It is optional and only valid if your API supports multiple languages.

{
  "error": {
    "code": 401,
    "message": {
      "en": "Unauthorized: Please log in to access this resource.",
      "es": "No autorizado: Por favor, inicie sesión para acceder a este recurso."
    }
  }
}

Hope this helps you to learn some techniques to provide meaningful error messages in your API response to help developers and users 🚀

Beginners to ProNode Js

Visual Guide to API Design Best Practices

This visual eBook covers essential best practices for designing robust APIs using REST principles.

This book is ideal for beginners and backend developers seeking to enhance their API design skills. However, it is not suited for those seeking an in-depth exploration of API design. This book is a quick read under 40 slides like scrolling through your instagram feed.

Visual Guide to API Design Best Practices