Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

OpenAI API: Response Structure

Description

Understanding the structure of API responses is essential for interpreting data returned from the OpenAI API. This tutorial explores the components and format of API responses.

HTTP Status Codes

HTTP status codes indicate the success or failure of a request. Commonly encountered status codes include:

  • 200 OK: The request was successful.
  • 400 Bad Request: The request was malformed.
  • 401 Unauthorized: Authentication is required or credentials are invalid.
  • 404 Not Found: The requested resource does not exist.
  • 500 Internal Server Error: An error occurred on the server side.

Example response with status code:

HTTP/1.1 200 OK
Content-Type: application/json

{ "id": "abcd1234", "text": "Hello, world!" }

Response Body

The response body contains the data returned by the API. It is typically in JSON format, but can vary based on the API endpoint.

Example JSON response body:

{ "id": "abcd1234", "text": "Hello, world!" }

Error Responses

Error responses provide details about issues encountered during a request. They often include error codes and messages to help diagnose problems.

Example error response:

HTTP/1.1 400 Bad Request
Content-Type: application/json

{ "error": "Invalid API key", "message": "Your API key is incorrect. Please check and try again." }

Conclusion

Understanding the response structure is crucial for effectively utilizing the OpenAI API. It ensures that you can interpret and handle responses appropriately based on the information provided.