Most business tools, from CRMs to accounting apps, share their data through an API: a web address that answers with data instead of a page. If someone on your team copies contacts from a CRM into a spreadsheet every week, that's a job for a few lines of JavaScript.

This walkthrough uses JSONPlaceholder, a free practice API with ten made-up users, and JSFiddle (no account needed). By the end you'll have a "Client contacts" table filled by your own code.

Step 1: look at the data first

Open this address in your browser:

https://jsonplaceholder.typicode.com/users 

You get ten users as JSON. Each one looks roughly like this (trimmed):

{ "id": 1, "name": "Leanne Graham", "email": "Sincere@april.biz", "company": { "name": "Romaguera-Crona" } } 

The three fields we want are name, email and the company's name. Note that the company name is inside company. That nesting matters later.

Always look at the raw response before writing code. Most "undefined" bugs come from guessing a field name.

Step 2: a heading and an empty table

In JSFiddle's HTML panel:

<h3>Client contacts</h3> <table id="contacts" border="1"></table> 

JSFiddle often adds closing tags as you type. If you end up with a doubled </table> or a stray >, delete it.

Step 3: the address and the table

In the JavaScript panel:

const url = 'https://jsonplaceholder.typicode.com/users'; const table = document.querySelector('#contacts'); 

querySelector('#contacts') finds the table by its id, so the code can write into it.

Step 4: fetch the data

Leave a blank line, then:

async function load() { const response = await fetch(url); const users = await response.json(); 

Three things happen here:

  • fetch(url) asks the address for the data
  • await waits for the answer, because asking another server takes a moment
  • .json() turns the response body into a JavaScript array you can loop over (it's also asynchronous, hence the second await)

await only works inside a function marked async, which is why everything lives inside load. Without await, the next line would try to use users before it arrived.

Step 5: header row and a loop

table.innerHTML = '<tr><th>Name</th><th>Company</th><th>Email</th></tr>'; for (const u of users) { 

The first line replaces whatever is in the table with a header row. The loop runs once per user.

Step 6: one row per contact

table.innerHTML += '<tr><td>' + u.name + '</td><td>' + u.company.name + '</td><td>' + u.email + '</td></tr>'; } } 

u.company.name reaches inside company for its name. If you wrote u.company, the cell would show [object Object]. If you wrote u.companyName, it would show undefined. Both are the classic symptoms of not looking at the JSON first.

The two closing braces end the loop and the function.

Step 7: call it

load(); 

Without this line the function exists but never runs.

The whole script

After clicking JSFiddle's tidy icon on the JavaScript panel, it reads:

const url = 'https://jsonplaceholder.typicode.com/users'; const table = document.querySelector('#contacts'); async function load() { const response = await fetch(url); const users = await response.json(); table.innerHTML = '<tr><th>Name</th><th>Company</th><th>Email</th></tr>'; for (const u of users) { table.innerHTML += '<tr><td>' + u.name + '</td><td>' + u.company.name + '</td><td>' + u.email + '</td></tr>'; } } load(); 

Click Run. The Result panel shows a Client contacts table with ten names, company names and emails, all read from the API.

Making it sturdier

The beginner version is deliberately short. Before you point it at anything real, three upgrades are worth knowing:

Check the response. fetch only rejects on network failure. A 404 or 500 still "succeeds", so check response.ok:

const response = await fetch(url); if (!response.ok) { table.innerHTML = '<tr><td>Could not load contacts (' + response.status + ')</td></tr>'; return; } 

Use textContent for data you don't control. Building HTML by string concatenation is fine for a practice API, but with real data a name like <img onerror=...> would be treated as HTML. Creating cells with document.createElement('td') and setting .textContent avoids that.

Build once, write once. innerHTML += re-parses the whole table on every loop. For ten rows nobody notices; for a few thousand, build the rows into one string (or a DocumentFragment) and write it once.

From practice API to a real CRM

A real CRM API works the same way, with one addition: a key that proves who you are, usually sent in a header:

fetch(url, { headers: { Authorization: 'Bearer ' + key } }); 

Never put a real key in public code like a shared fiddle or a public web page. Anyone can read it in the browser. For anything real, the key belongs on a server or in an automation tool that stores credentials for you.

Checklist

  • [ ] Opened the API address and read the raw JSON first
  • [ ] Table has an id the script can find
  • [ ] await used inside an async function, for both fetch and .json()
  • [ ] Nested fields read with dot notation (u.company.name)
  • [ ] load() actually called
  • [ ] response.ok checked before using the data
  • [ ] No real API key in browser code

Practice it

This is based on a free step-by-step lesson where you type every line yourself in a practice copy of JSFiddle that checks your work (9 steps, about 12 minutes): JavaScript: Pull Contacts from an API into a Table.