Secure API Design: Fundamentals for Safe and Scalable Systems Quiz

Test your understanding of secure API design principles, including resource modeling, input validation, sanitization, idempotency, rate limiting, and versioning. This quiz helps you assess knowledge of best practices for building secure and reliable APIs.

  1. Resource Modeling Best Practices

    When designing an API to manage products in an online store, which approach best follows resource-oriented modeling?

    1. Represent each product as a distinct resource like /products/{id}
    2. Allow direct manipulation of database tables from the client
    3. Store resources using system-specific internal IDs only
    4. Use a single endpoint for all store operations, such as /store

    Explanation: Representing each product as a distinct resource like /products/{id} is aligned with resource-oriented modeling, making APIs intuitive and easily understood. Allowing direct manipulation of database tables exposes implementation details and security risks. Using a single endpoint for all operations defeats the clarity and organization of resource modeling. Storing only system-specific internal IDs reduces the clarity and flexibility for clients interacting with resources.

  2. Input Validation in APIs

    Why should an API validate all client input before processing requests such as user sign-ups?

    1. To prevent malicious data and preserve system integrity
    2. To decrease performance by adding checks
    3. To allow clients to send any kind of information
    4. To make API endpoints more complicated

    Explanation: Validating input protects against malicious data that could exploit system vulnerabilities and helps maintain system integrity. Making endpoints more complicated is not a goal of validation; it simply results from improper implementation. Although input validation can have a small performance impact, it does not exist to decrease performance. Allowing clients to send any kind of information is risky and not secure.

  3. Sanitization Versus Validation

    What is the main difference between input validation and input sanitization in an API?

    1. Sanitization only works on database fields
    2. Validation checks data correctness; sanitization cleans unsafe data
    3. Validation encrypts data; sanitization does not
    4. Sanitization makes data faster; validation makes data slower

    Explanation: Validation is used to check if data is correct and meets required rules, while sanitization is used to clean or remove potentially unsafe parts from data, such as scripts or unwanted characters. Sanitization does not make data faster; this is a misconception. Validation does not encrypt data, and sanitization is not limited to database fields—it applies to input data regardless of its use.

  4. The Role of Idempotency

    Why is it important for certain API operations, such as payment processing, to be idempotent?

    1. Idempotency prevents clients from accessing resources
    2. Idempotency requires APIs to store all old requests forever
    3. Idempotency ensures that repeating the same request produces the same result without unintended effects
    4. Idempotency always makes APIs run faster

    Explanation: Idempotency protects against issues like duplicate payments by ensuring that repeating a request doesn't cause unintended consequences. Idempotency does not always improve speed; it focuses on predictable outcomes. It does not block client access to resources. Requiring APIs to keep all old requests forever is not accurate; idempotency strategies typically use keys or tokens rather than indefinite storage.

  5. Implementing Rate Limiting

    What is a practical reason for using rate limiting in a public API?

    1. To force clients to wait for every response, even for simple requests
    2. To encrypt data between client and server
    3. To reduce risk of abuse and prevent system overload by limiting client requests
    4. To ensure only one client can ever access the API at a time

    Explanation: Rate limiting helps protect APIs from abuse and ensures stability by controlling how many requests a client can make in a given period. It does not enforce exclusive access for a single client. Rate limiting is independent of data encryption between client and server. The purpose is to regulate request volume, not to deliberately slow down simple requests.

  6. API Versioning Purpose

    Why is versioning essential when updating breaking changes in an API's data structure or behavior?

    1. Versioning automatically fixes security vulnerabilities in the code
    2. Versioning eliminates all possible bugs in the API
    3. Versioning removes the need for any documentation
    4. Versioning lets clients continue using old versions while new changes are adopted

    Explanation: Versioning allows clients to keep using a stable, known API version while adopters can migrate to new changes, which helps prevent disruptions. It does not automatically resolve security vulnerabilities or eliminate bugs. While it improves manageability, versioning does not make documentation unnecessary; clear documentation remains crucial.

  7. Resource Identifiers in RESTful APIs

    Which option best describes how resources should be uniquely identified in a well-designed RESTful API?

    1. By using clear, predictable URIs such as /users/123
    2. By randomly changing the location for each resource
    3. By requiring clients to guess resource URIs
    4. By hiding all identifiers and only returning lists

    Explanation: Using clear, predictable URIs for each resource allows clients to interact with resources in a straightforward and consistent way. Randomly changing locations confuses clients and undermines REST principles. Hiding identifiers or requiring clients to guess URIs removes transparency and usability from the API design.

  8. Preventing Injection Attacks

    How does input validation and sanitization help protect APIs from common security threats like injection attacks?

    1. They block or clean harmful inputs before reaching the backend logic
    2. They prevent the need to log any user activity
    3. They make the user interface look more modern
    4. They automatically generate documentation for endpoints

    Explanation: Input validation and sanitization help stop dangerous data from reaching sensitive backend operations, thereby reducing the chance of injection attacks. Improving the user interface design is unrelated to these protections. Documentation generation and user activity logging are separate concerns from data protection.

  9. Idempotency Key Usage Example

    If a client accidentally sends the same payment request twice due to a network issue, what feature helps prevent double processing in a secure API?

    1. Removing all authentication from the API
    2. Requiring an idempotency key in the request header
    3. Changing the endpoint URL on every request
    4. Allowing only GET requests for all actions

    Explanation: An idempotency key allows the server to identify and ignore repeated requests, ensuring a payment is only processed once. Removing authentication weakens security, and changing URLs does not solve the issue of duplicated requests. Allowing only GET requests prevents operations like payments from occurring at all, which is not practical.

  10. Versioning Example in API URLs

    Which API endpoint demonstrates best practice for implementing versioning in the URL?

    1. /users/get
    2. /v1/users
    3. /latest-users
    4. /users/old

    Explanation: Including the version number in the URL, such as /v1/users, is a common and clear method for versioning. /users/get and /users/old do not communicate the API version explicitly. /latest-users refers to data freshness but not versioning. Proper versioning helps clients easily identify which version of the API they are using.