Hone logo
Hone
Problems

Build a Simple JSON API Server in Python

This challenge focuses on building a basic JSON API server using Python. You will learn how to handle HTTP requests, process data, and return responses in JSON format, which is fundamental for modern web development and inter-application communication.

Problem Description

Your task is to create a Python application that acts as a simple RESTful API server. This server should be able to:

  1. Handle GET requests: Retrieve data based on provided identifiers.
  2. Handle POST requests: Add new data to the server's internal storage.
  3. Respond with JSON: All responses should be formatted as JSON.
  4. Maintain internal state: The server needs to store data in memory, which will be lost when the server restarts.

You will implement an API to manage a collection of "items." Each item will have a unique id and a name.

Key Requirements:

  • Use a Python web framework (e.g., Flask, FastAPI) to handle HTTP requests.
  • Implement an endpoint for retrieving a specific item by its id.
  • Implement an endpoint for creating a new item.
  • The server should maintain a list or dictionary of items in memory.
  • All responses must adhere to the JSON API specification for basic data structures (resource objects).

Expected Behavior:

  • GET /items/{id}: If an item with the given id exists, return a JSON object representing that item. If not found, return a 404 Not Found error.
  • POST /items: Accept a JSON payload containing a new item's name. The server should assign a unique id (you can use a simple counter) and add the item to its collection. Return the newly created item's JSON representation with its assigned id and a 201 Created status code.
  • Error Handling: For invalid requests (e.g., missing data in POST, non-existent ID in GET), return appropriate HTTP status codes and informative JSON error messages.

Edge Cases to Consider:

  • What happens if a POST request does not contain a name field?
  • What happens if a GET request is made for an ID that does not exist?
  • How will you ensure unique IDs for new items?

Examples

Example 1: Retrieving an existing item

Request:
GET /items/1

Response:
Status Code: 200 OK
Content-Type: application/json

{
  "data": {
    "type": "items",
    "id": "1",
    "attributes": {
      "name": "Laptop"
    }
  }
}

Explanation: The server successfully found the item with ID "1" and returned its data in the specified JSON API format.

Example 2: Creating a new item

Request:
POST /items
Content-Type: application/json

{
  "data": {
    "type": "items",
    "attributes": {
      "name": "Keyboard"
    }
  }
}

Response:
Status Code: 201 Created
Content-Type: application/json

{
  "data": {
    "type": "items",
    "id": "2",
    "attributes": {
      "name": "Keyboard"
    }
  }
}

Explanation: A new item "Keyboard" was successfully created. The server assigned it a new ID ("2") and returned its representation.

Example 3: Item not found

Request:
GET /items/99

Response:
Status Code: 404 Not Found
Content-Type: application/json

{
  "errors": [
    {
      "status": "404",
      "title": "Not Found",
      "detail": "Item with ID 99 not found."
    }
  ]
}

Explanation: The requested item with ID "99" does not exist, so the server returned a 404 error with a descriptive message.

Constraints

  • Your API should run on a local development server.
  • The internal data storage should be a simple Python dictionary or list in memory. No external databases are required.
  • The API should support application/json content type for both requests and responses.
  • IDs for items should be represented as strings.
  • The maximum number of items stored in memory should not exceed 1000 for this challenge.
  • The name attribute of an item must be a string and cannot be empty.

Notes

  • You are free to choose any Python web framework you are comfortable with (e.g., Flask, FastAPI). Flask is generally simpler for beginners.
  • Familiarize yourself with the basic structure of JSON API: resource objects, attributes, relationships, errors. For this challenge, focus on resource objects and basic error structures.
  • Consider how you will generate unique IDs for new items. A simple counter that increments with each new item is a good starting point.
  • Pay attention to HTTP status codes (200, 201, 404, 400) as they are crucial for RESTful APIs.
  • Think about how to parse incoming JSON data and how to serialize Python data structures into JSON for responses.
Loading editor...
python