Essential API Design Practices for Generative AI Endpoints Quiz

Test your knowledge of key API design fundamentals for generative AI, including resource modeling, input validation, versioning, and idempotency. Explore best practices and common scenarios to build robust and scalable AI-powered APIs.

  1. Resource Identification

    When designing an API endpoint for a text-generating AI, which best represents a suitable resource name according to RESTful conventions?

    1. /generateTextNow
    2. /text_output
    3. /TextGenAPI
    4. /texts

    Explanation: /texts follows the RESTful approach of using plural nouns to represent collections of resources, making it the most appropriate. /generateTextNow is action-based, which is less RESTful. /text_output is singular and doesn't indicate a collection. /TextGenAPI refers to the whole API and not a specific resource.

  2. Input Validation

    Why is input validation important when designing an endpoint that accepts user-provided prompts for generative AI?

    1. It speeds up model inference time directly
    2. It prevents invalid or harmful data from affecting model outputs
    3. It increases the size of API responses
    4. It eliminates the need for authentication

    Explanation: Input validation ensures that only appropriate and safe data is processed by your generative AI, preventing security and quality issues. It does not impact model speed, response size, or replace authentication requirements, all of which are different concerns.

  3. Versioning APIs

    Which approach is commonly used to version a generative AI API in the endpoint design?

    1. /v1/texts
    2. /latest_texts
    3. /texts/versionone
    4. /texts-v1.0.0

    Explanation: /v1/texts places the version number as a prefix, which is a widely accepted practice. /texts-v1.0.0 and /texts/versionone use less standard and potentially confusing notation. /latest_texts is ambiguous because 'latest' may change over time and doesn't specify a fixed version.

  4. Idempotent Methods

    For a generative AI API, which HTTP method is NOT considered idempotent?

    1. PUT
    2. DELETE
    3. GET
    4. POST

    Explanation: POST is not idempotent because multiple identical POST requests may result in multiple entries or generations, causing different results each time. GET, PUT, and DELETE are generally idempotent since repeating them yields the same outcome.

  5. Handling Prompt Length

    What is a best practice for input validation regarding prompt length in a generative AI endpoint?

    1. Allow unlimited prompt length for all users
    2. Truncate prompts without notifying users
    3. Reject prompts that exceed a defined maximum length
    4. Accept prompts only if they are empty

    Explanation: Rejecting prompts that are too long ensures system stability and predictable processing. Allowing unlimited length can lead to resource exhaustion. Truncating without notification may confuse users. Accepting only empty prompts defeats the endpoint's purpose.

  6. Describing Response Structure

    Why should API documentation for generative AI endpoints clearly describe response structures?

    1. To control how the AI generates data
    2. To hide error messages
    3. So developers know what data to expect and how to handle it
    4. To increase network bandwidth

    Explanation: Clear response documentation helps developers parse and use API results effectively. It does not affect bandwidth or AI generation logic, nor does it serve to hide errors.

  7. Accepting File Uploads

    If your generative AI endpoint accepts files as input, which content type should you specify to support file uploads?

    1. text/plain
    2. multipart/form-data
    3. application/octetstram
    4. application/xml

    Explanation: multipart/form-data is the standard for handling file uploads in HTTP requests. text/plain is for plain text only. application/octetstram is a misspelling of application/octet-stream and is not a valid type. application/xml is for XML data, not file uploads.

  8. Resource Hierarchy Example

    Which API design shows a hierarchical nesting of resources suitable for user-specific generative results?

    1. /getGeneratedText
    2. /results_text
    3. /users/123/texts
    4. /user-text-output

    Explanation: /users/123/texts uses a clear hierarchy, indicating texts generated by a specific user. /results_text and /user-text-output aren't structured and miss the nesting. /getGeneratedText is action-oriented, not resource-based.

  9. Input Data Types

    What input data type is best for a prompt field when accepting text for a generative AI endpoint?

    1. string
    2. boolean
    3. array
    4. integer

    Explanation: Prompts are naturally strings of text. Integer and boolean are not suitable for handling text data. Array may be used for multiple prompts, but not for a single prompt field.

  10. Ensuring Idempotency with Headers

    What header can be used to help ensure idempotency for POST requests in a generative AI API?

    1. Idempotency-Key
    2. Authorization
    3. Accept-Language
    4. Content-Length

    Explanation: The Idempotency-Key header allows servers to recognize repeated requests and prevent duplicate operations. Authorization is for security, Content-Length specifies body size, and Accept-Language handles localization.

  11. API Version Sunsetting

    What should an API provider do before discontinuing an old version of a generative AI endpoint?

    1. Immediately return errors without warning
    2. Notify users in advance about the planned change
    3. Delete all stored data for all users
    4. Reduce system capacity without notice

    Explanation: Advance notification allows users to migrate to newer API versions. Returning errors or reducing capacity without notice disrupts user experience. Deleting all data is unrelated to versioning and can violate user trust.

  12. Handling Malformed JSON

    If a request includes improperly formatted JSON input, what is the correct API behavior?

    1. Convert the input to XML automatically
    2. Return a 400 Bad Request error
    3. Send a 500 Internal Server Error
    4. Ignore and process the input anyway

    Explanation: A 400 Bad Request clearly indicates client-side input issues. A 500 error implies a server malfunction. Ignoring format problems risks unpredictable results, and auto-converting to XML is not standard API behavior.

  13. Singular vs. Plural Resource Naming

    Why are plural nouns recommended when naming resources in RESTful generative AI APIs (e.g., /outputs vs. /output)?

    1. Singular nouns are not allowed in REST
    2. Plurality determines the HTTP method used
    3. Plural names represent collections of resources, aligning with REST conventions
    4. Plural nouns make endpoints private

    Explanation: Using plural resource names signals that the endpoint deals with a collection rather than a single item, matching REST guidelines. Plurality does not make endpoints private or dictate HTTP methods, and singular forms are technically allowed but less conventional.

  14. Rate Limiting in APIs

    Why should generative AI endpoints implement rate limiting as part of their API design?

    1. To speed up requests for all users
    2. To remove authentication requirements
    3. To prevent abuse and manage system resources effectively
    4. To guarantee unlimited generations per user

    Explanation: Rate limiting helps protect APIs from overuse and ensures fair access. It does not inherently speed up operations or grant users unlimited access. It also does not replace authentication mechanisms.

  15. API Response Codes

    If a generative AI API successfully returns generated text, what status code should be used in the response?

    1. 403 Forbidden
    2. 201 Created
    3. 404 Not Found
    4. 200 OK

    Explanation: A 200 OK status indicates the request was successful and content is returned. 404 Not Found signals a missing resource, 403 Forbidden means access was denied, and 201 Created is typically for creating new resources, not generating or retrieving results.

  16. Consistent Error Responses

    Why is it important to use a consistent error response format in generative AI API endpoints?

    1. It makes the API invisible to users
    2. It increases randomization in responses
    3. It allows clients to reliably parse and present error details
    4. It guarantees models will never fail

    Explanation: Consistent error formats help client apps handle errors systematically. Randomizing or hiding errors impedes troubleshooting, and standardized responses cannot guarantee model success—they just help with communication.