Made with Palestinian hands Support EmuPath ☕

No results found.

Try another search or remove some filters.

← Practical lessons

Inspect an API response before building an app

By EmuPath · Reviewed November 24, 2026 · Local sample-data exercise

You are building an inventory display. A response arrives successfully, but the page incorrectly reports an item as missing. Before changing the interface, separate transport, syntax and application meaning. This exercise uses invented product data; it sends no API request and needs no account or token.

1. Preserve a small, non-sensitive sample

{"items":[{"sku":"SW-8","stock":0,"active":true},{"sku":"AP-2","stock":5,"active":false}],"next":null}

Copy the sample into the JSON formatter. Formatting should reveal an object with an items array containing two objects and a next field set to null. The formatter can check syntax; it cannot know the inventory rules your application intends.

In this fictional contract, stock is a nonnegative integer, active controls whether an item may be offered, and next is either a continuation value or null. We define “available” as active with stock greater than zero. Neither row is available: SW-8 is active but has no stock; AP-2 has stock but is inactive. A UI that tests only one field will be wrong.

2. Separate missing, zero and null

The first item’s stock is present and equals zero. It is not missing. A fallback such as stock || 'unknown' treats zero as false and can display a misleading label. Check the contract and value type explicitly. A nullish fallback can preserve zero, but still does not validate that the value is a number or in range.

For this contract, null in next means there is no next page. A missing field could instead mean the response is incomplete or the provider changed versions. Do not assign the same meaning to absent, null, empty string and zero unless the API documentation does so.

3. Try two controlled failures

  1. Add a comma after "next":null before the closing brace. Standard JSON rejects that trailing comma. Remove it and confirm the formatter accepts the sample again.
  2. Change the first stock value from 0 to "0". This is still valid JSON, but it violates our numeric contract. A syntax-only success is not a data-validation success.

For an actual integration, validate the object shape, required fields, types and bounds before using data in calculations. Treat unknown additional fields according to the versioning contract. Do not turn remote strings into executable code or render them as trusted HTML.

4. Inspect the response before parsing it

An API request can return an HTML login page, a rate-limit error or a different response shape. Record the HTTP status, content type and a safe excerpt of the body. Do not expose authorization headers or personal records in a bug report. A successful network request does not guarantee that the operation succeeded or that its body matches your application’s model.

Handle an empty successful response according to its contract instead of always calling a JSON parser. For a rate limit, follow the provider’s retry guidance and use bounded retries. Repeating a state-changing request may duplicate an operation unless the API offers an idempotency mechanism.

5. Distinguish CORS from authentication

If a request works from a server-side client but fails in a browser, inspect the browser console and response headers. CORS controls whether the browser may expose a cross-origin response to your page. It is not an API password, and it does not prevent every request from reaching the server.

Setting fetch to no-cors is not a general fix for reading API JSON: the resulting opaque response is not readable by your application. Use a provider-supported browser integration or a backend you control where appropriate. Never publish a secret server API key in frontend code, even if the provider’s CORS policy permits the request.

The JSON formatter accepts stock as the string "0". Is the sample ready for the inventory calculation?

A useful integration record

Use the API directory to find documentation, then confirm these details at the provider. A community listing’s HTTPS, authentication or CORS labels are leads to investigate, not a service-level guarantee.

References and scope

The inventory contract and exercise are original examples. JSON grammar is specified in RFC 8259. Browser cross-origin behavior is described in MDN’s CORS guide. This lesson does not certify a particular provider. Report a factual error with a non-sensitive sample.