Getting started overview

Gorest offers a free, publicly available REST API intended for testing and development. It provides a set of endpoints that simulate common CRUD (Create, Read, Update, Delete) operations on resources like users, posts, comments, and todos. This allows developers to quickly integrate and test frontend applications, practice API interactions, or demonstrate concepts without the need to build or deploy a custom backend service.

The primary benefit of using Gorest for getting started is its minimal setup requirement. Read operations (GET requests) generally do not require authentication, allowing immediate data retrieval. For write (POST, PUT, DELETE) operations, a free API access token is necessary to authorize modifications to the simulated data. The API is designed to be straightforward, with clear documentation outlining available resources and expected request/response formats.

This guide will walk through the process of obtaining an API token and making your first authenticated request to Gorest, ensuring you can perform both read and write operations. Understanding the basics of RESTful APIs, including HTTP methods and status codes, will be beneficial when working with Gorest, as described in the MDN Web Docs on HTTP status codes.

Gorest Getting Started Quick Reference
Step What to Do Where
1. Sign Up Register for a free account to obtain an API access token. Gorest REST API homepage
2. Get API Key Generate a new access token from your profile dashboard. Gorest user dashboard
3. Make Request Construct an HTTP request using your token for authentication. Any HTTP client (e.g., cURL, Postman, browser fetch)
4. Explore Endpoints Review available API endpoints for users, posts, comments, and todos. Gorest API documentation

Create an account and get keys

To interact with Gorest, particularly for creating, updating, or deleting resources, you will need an API access token. Gorest operates on a token-based authentication system, where a unique token identifies and authorizes your requests.

Follow these steps to create an account and obtain your API key:

  1. Visit the Gorest Homepage: Navigate to the official Gorest REST API documentation.
  2. Register for an Account: Look for a "Sign Up" or "Register" option. You will typically need to provide an email address and create a password.
  3. Verify Email (If Required): Some platforms require email verification before full account access. Check your inbox for a verification link.
  4. Log In: Once registered and verified, log in to your Gorest account.
  5. Generate an Access Token: After logging in, you should be directed to a dashboard or profile page. On this page, there will be an option to generate a new "Access Token." Click this option. Gorest typically provides a long string of characters as your token.
  6. Secure Your Token: Copy your generated access token immediately. This token acts as your credentials for authenticated requests. Treat it like a password and keep it secure. Gorest tokens are typically "Bearer" tokens, which are a common type of OAuth 2.0 access token.

Gorest allows you to generate multiple tokens if needed, and you can revoke old tokens from your dashboard. For development, it's common practice to use environment variables to store API keys rather than hardcoding them directly into your application code.

Your first request

With an API access token in hand, you can now make your first authenticated request to Gorest. This example demonstrates creating a new user, which requires authentication.

Before making a POST request, you can perform a GET request to retrieve existing users without authentication to verify connectivity:

curl "https://gorest.co.in/public/v2/users"

This command should return a JSON array of user objects. Now, let's create a new user using your access token. Replace YOUR_ACCESS_TOKEN with the token you generated.

curl -i -H "Accept:application/json" \
     -H "Content-Type:application/json" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -X POST "https://gorest.co.in/public/v2/users" \
     -d '{"name":"API Spine User", "gender":"male", "email":"[email protected]", "status":"active"}'

Explanation of the cURL command:

  • -i: Includes the response headers in the output.
  • -H "Accept:application/json": Tells the server to respond with JSON.
  • -H "Content-Type:application/json": Informs the server that the request body is JSON.
  • -H "Authorization: Bearer YOUR_ACCESS_TOKEN": This is where your access token is included. The Bearer prefix is standard for this type of token.
  • -X POST: Specifies the HTTP method as POST, indicating a creation operation.
  • "https://gorest.co.in/public/v2/users": The target endpoint for creating a new user.
  • -d '{"name":"API Spine User", "gender":"male", "email":"[email protected]", "status":"active"}': The request body containing the data for the new user, formatted as JSON. Ensure the email is unique, or the request may fail with a 422 Unprocessable Entity error.

A successful request will typically return an HTTP 201 Created status code and the JSON representation of the newly created user, including its unique ID. You can verify the creation by making another GET request to the users endpoint or specifically to the new user's ID.

Common next steps

After successfully making your first request, consider these common next steps to further explore Gorest and integrate it into your development workflow:

  1. Explore Other Endpoints: Review the Gorest API documentation to understand the available endpoints for posts, comments, and todos. Experiment with GET, PUT, and DELETE requests on these resources.
  2. Implement CRUD Operations: Practice implementing all four CRUD operations (Create, Read, Update, Delete) for a specific resource type. For example, create a user, retrieve that user, update their details, and then delete the user.
  3. Handle Error Responses: Gorest provides various HTTP status codes and error messages for invalid requests (e.g., 401 Unauthorized, 404 Not Found, 422 Unprocessable Entity). Implement error handling in your code to gracefully manage these responses.
  4. Integrate with a Frontend: If you are building a frontend application, integrate Gorest as your mock backend. This allows you to develop UI components that interact with an API without waiting for a production backend to be ready.
  5. Use an API Client: While cURL is excellent for quick tests, consider using a dedicated API client like Postman or Insomnia for more complex testing, environment management, and request history. These tools can simplify the process of constructing and sending requests.
  6. Automate Tests: For continuous integration and robust development, write automated tests that interact with the Gorest API. This ensures your application's API interactions remain functional as you make changes.
  7. Learn About Rate Limiting: While Gorest is free, public APIs often have rate limits to prevent abuse. Although not explicitly detailed on the main Gorest documentation, it's a good practice to be aware of how rate limiting works when interacting with any public API.

Troubleshooting the first call

If your first API call to Gorest doesn't work as expected, here are some common issues and troubleshooting steps:

  • 401 Unauthorized:
    • Missing Token: Ensure you have included the Authorization: Bearer YOUR_ACCESS_TOKEN header in your request.
    • Invalid Token: Double-check that the token you are using is correct and hasn't expired or been revoked. Generate a new token from your Gorest dashboard if unsure.
    • Incorrect Prefix: Verify that your token is prefixed with Bearer, followed by a space.
  • 404 Not Found:
    • Incorrect URL: Check the endpoint URL for typos. Ensure it matches the paths specified in the Gorest API documentation.
    • Wrong HTTP Method: Ensure you are using the correct HTTP method (e.g., POST for creating, GET for retrieving).
  • 422 Unprocessable Entity:
    • Invalid Request Body: The data you sent in the request body is likely malformed or missing required fields. For example, when creating a user, fields like name, email, gender, and status are typically required.
    • Duplicate Unique Field: For user creation, the email field must be unique. If you try to create a user with an email that already exists, you will receive this error. Try a different email address.
    • Incorrect Content-Type: Ensure the Content-Type: application/json header is set if you're sending a JSON body.
  • Network Issues:
    • Internet Connection: Verify your internet connection is stable.
    • Firewall/Proxy: If you are on a corporate network, a firewall or proxy might be blocking your requests. Try from a different network if possible.
  • cURL Syntax Errors:
    • Quotes: Ensure all quotes are correctly matched, especially for the -d (data) and -H (header) flags.
    • Backslashes: If using backslashes for line continuation in your terminal, ensure they are correctly placed and followed by a newline.

Always refer to the specific error message in the API response, as it often provides precise details about what went wrong. The Gorest API reference is the definitive source for endpoint specifics and expected parameters.