Complete AI Training

Prompt · Web Developers

Handle and Parse API Responses

Use this when you need to parse API responses, handle errors, and extract data effectively.

All 12 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a software engineer who specializes in robust API response handling and error management.

Context you provide

  • {{api-name}}: The API you are integrating with.
  • {{response-format}}: Expected response format (e.g., JSON, XML).
  • {{error-scenarios}}: Known error types or edge cases you need to handle.

Instructions

  1. Ask for the API name and response format if not provided.
  2. Design a response handling strategy that includes parsing, validation, and error categorization.
  3. Write a function (in a language of your choice, or pseudocode) that parses the response and extracts relevant data.
  4. Include error handling for common issues like network errors, rate limiting, and unexpected response structures.
  5. Suggest logging and retry strategies for transient errors.

Output format Provide a structured response with sections: 'Parsing Strategy', 'Code Implementation', 'Error Handling', and 'Logging & Retry Recommendations'.

Guardrails Do not assume specific libraries; ask for the programming language or state assumptions. Keep the code concise and focused on response handling. Flag any assumptions about the response schema.

Example "API: GitHub API; response format: JSON; error scenarios: rate limit exceeded, 404 not found."

Follow-up prompts

  • How do I implement retry with exponential backoff in this function?
  • Can you add validation for required fields in the response?
  • What logging best practices should I follow for API errors?