Below is one complete example. In this example we assume that the API documented at
https://documenter.getpostman.com/view/664302/S1ENwy59
provides “user‐related” endpoints (list users, get a single user, create a user, update a user, and delete a user). (If your API has different endpoints, simply adjust the endpoint URLs and parameters accordingly.)
We’ll first create several helper “wrapper” functions that use JavaScript’s Fetch API to call the API. Then we build a simple Single‑Page Application (SPA) using one HTML file (with inline CSS and JavaScript) that lets you call each function and view the returned data.
You can copy the following code into a file (say, index.html) and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>API SPA Example</title>
<style>
/* Basic CSS styling for the SPA */
body {
font-family: Arial, sans-serif;
max-width: 1000px;
margin: 2em auto;
background: #f9f9f9;
padding: 1em;
line-height: 1.6;
}
h1, h2 {
color: #333;
}
.container {
margin-bottom: 2em;
background: #fff;
padding: 1em;
border: 1px solid #ddd;
border-radius: 4px;
}
.result {
margin-top: 1em;
padding: 0.5em;
background: #eef;
border: 1px solid #ccd;
white-space: pre-wrap;
}
label {
display: block;
margin-top: 0.5em;
}
input, textarea {
width: 100%;
padding: 0.4em;
margin-top: 0.2em;
box-sizing: border-box;
}
button {
margin-top: 0.5em;
padding: 0.5em 1em;
font-size: 1em;
cursor: pointer;
}
nav a {
margin-right: 1em;
text-decoration: none;
color: #0066cc;
}
nav a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>API SPA Example</h1>
<p>This SPA calls the API functions as documented at
<a href="https://documenter.getpostman.com/view/664302/S1ENwy59" target="_blank">
Postman Documentation</a>.
</p>
<!-- Navigation links to each section of our SPA -->
<nav>
<a href="#listUsers">List Users</a>
<a href="#getUser">Get User</a>
<a href="#createUser">Create User</a>
<a href="#updateUser">Update User</a>
<a href="#deleteUser">Delete User</a>
</nav>
<!-- Section: List all Users -->
<section class="container" id="listUsers">
<h2>List All Users</h2>
<button id="listUsersBtn">Load Users</button>
<div id="listUsersResult" class="result"></div>
</section>
<!-- Section: Get Single User -->
<section class="container" id="getUser">
<h2>Get Single User</h2>
<label for="getUserId">User ID:</label>
<input type="text" id="getUserId" placeholder="Enter user id">
<button id="getUserBtn">Get User</button>
<div id="getUserResult" class="result"></div>
</section>
<!-- Section: Create User -->
<section class="container" id="createUser">
<h2>Create User</h2>
<form id="createUserForm">
<label for="createName">Name:</label>
<input type="text" id="createName" required>
<label for="createEmail">Email:</label>
<input type="email" id="createEmail" required>
<button type="submit">Create User</button>
</form>
<div id="createUserResult" class="result"></div>
</section>
<!-- Section: Update User -->
<section class="container" id="updateUser">
<h2>Update User</h2>
<form id="updateUserForm">
<label for="updateUserId">User ID:</label>
<input type="text" id="updateUserId" required>
<label for="updateName">Name:</label>
<input type="text" id="updateName">
<label for="updateEmail">Email:</label>
<input type="email" id="updateEmail">
<button type="submit">Update User</button>
</form>
<div id="updateUserResult" class="result"></div>
</section>
<!-- Section: Delete User -->
<section class="container" id="deleteUser">
<h2>Delete User</h2>
<label for="deleteUserId">User ID:</label>
<input type="text" id="deleteUserId" placeholder="Enter user id">
<button id="deleteUserBtn">Delete User</button>
<div id="deleteUserResult" class="result"></div>
</section>
<script>
/*********************************************
* API Wrapper Functions using Fetch API
*********************************************/
// NOTE: Set the API_BASE_URL per your API's host (this may be provided in your Postman docs)
const API_BASE_URL = "https://your.api.base.url";
// GET all users
function getUsers() {
return fetch(`${API_BASE_URL}/users`)
.then(response => {
if (!response.ok) {
throw new Error("Error fetching users");
}
return response.json();
});
}
// GET a single user by id
function getUser(id) {
return fetch(`${API_BASE_URL}/users/${id}`)
.then(response => {
if (!response.ok) {
throw new Error("Error fetching user");
}
return response.json();
});
}
// POST create new user (data: {name, email})
function createUser(userData) {
return fetch(`${API_BASE_URL}/users`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(userData)
}).then(response => {
if (!response.ok) {
throw new Error("Error creating user");
}
return response.json();
});
}
// PUT update an existing user by id
function updateUser(id, userData) {
return fetch(`${API_BASE_URL}/users/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(userData)
}).then(response => {
if (!response.ok) {
throw new Error("Error updating user");
}
return response.json();
});
}
// DELETE a user by id
function deleteUser(id) {
return fetch(`${API_BASE_URL}/users/${id}`, {
method: "DELETE"
}).then(response => {
if (!response.ok) {
throw new Error("Error deleting user");
}
return response.json();
});
}
/*********************************************
* SPA Event Listeners and Form Handling
*********************************************/
document.addEventListener("DOMContentLoaded", () => {
// List All Users
document.getElementById("listUsersBtn").addEventListener("click", () => {
const resultDiv = document.getElementById("listUsersResult");
resultDiv.textContent = "Loading...";
getUsers()
.then(data => {
resultDiv.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
resultDiv.textContent = error.toString();
});
});
// Get Single User
document.getElementById("getUserBtn").addEventListener("click", () => {
const userId = document.getElementById("getUserId").value.trim();
const resultDiv = document.getElementById("getUserResult");
if (!userId) {
resultDiv.textContent = "Please provide a user id.";
return;
}
resultDiv.textContent = "Loading...";
getUser(userId)
.then(data => {
resultDiv.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
resultDiv.textContent = error.toString();
});
});
// Create User
document.getElementById("createUserForm").addEventListener("submit", (e) => {
e.preventDefault();
const name = document.getElementById("createName").value.trim();
const email = document.getElementById("createEmail").value.trim();
const resultDiv = document.getElementById("createUserResult");
// Simple validation
if (!name || !email) {
resultDiv.textContent = "Name and Email are required.";
return;
}
resultDiv.textContent = "Creating user...";
createUser({ name, email })
.then(data => {
resultDiv.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
resultDiv.textContent = error.toString();
});
});
// Update User
document.getElementById("updateUserForm").addEventListener("submit", (e) => {
e.preventDefault();
const id = document.getElementById("updateUserId").value.trim();
const name = document.getElementById("updateName").value.trim();
const email = document.getElementById("updateEmail").value.trim();
const resultDiv = document.getElementById("updateUserResult");
if (!id) {
resultDiv.textContent = "User id is required.";
return;
}
// Build the update payload (only include fields if provided)
const payload = {};
if (name) payload.name = name;
if (email) payload.email = email;
if (Object.keys(payload).length === 0) {
resultDiv.textContent = "Please provide at least one field to update.";
return;
}
resultDiv.textContent = "Updating user...";
updateUser(id, payload)
.then(data => {
resultDiv.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
resultDiv.textContent = error.toString();
});
});
// Delete User
document.getElementById("deleteUserBtn").addEventListener("click", () => {
const userId = document.getElementById("deleteUserId").value.trim();
const resultDiv = document.getElementById("deleteUserResult");
if (!userId) {
resultDiv.textContent = "Please provide a user id.";
return;
}
resultDiv.textContent = "Deleting user...";
deleteUser(userId)
.then(data => {
resultDiv.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
resultDiv.textContent = error.toString();
});
});
});
</script>
</body>
</html>
How It Works
-
Wrapper Functions:
- Functions such as
getUsers(), getUser(id), createUser(userData), updateUser(id, userData) and deleteUser(id) are defined at the top. They use the Fetch API to perform HTTP GET, POST, PUT, and DELETE requests respectively.
- Make sure you update the value of
API_BASE_URL to match the correct API host from your Postman documentation.
-
HTML Structure:
- The HTML page is divided into sections (wrapped in
<section> tags) for listing users, getting a single user, creating, updating, and deleting users.
- Each section contains a button and (where appropriate) a form so that the user can enter parameters and initiate the corresponding function call.
-
Event Listeners:
- When the page loads (
DOMContentLoaded), we add listeners to buttons and forms. For example, when the “Load Users” button is clicked, it calls getUsers() and prints the returned JSON into the <div> with id "listUsersResult".
-
Styling:
- Basic CSS styling is provided inline at the top inside the
<style> tag.
-
Testing the SPA:
- To try out the SPA, open the HTML file in your browser. Use the forms and buttons to call the API endpoints. The raw JSON responses (or error messages) will be printed in the result boxes.
If your Postman documentation describes endpoints other than the ones provided above (for example, for a different resource than “users”), simply adjust the endpoint URL paths and the data payloads accordingly. This example gives you a foundation both for wrapping your API calls via fetch and for creating a small single‑page interface to call them.