What you’ll build
- A hosted GET /v1/users/:id route and static profile response.
- A typed fetchUser() client driven by VITE_API_URL.
- Loading, error, success, and not-found interface states.
- A repeatable checklist for the complete flow.
01
Create the project and profile route
Create a Mockport project named customer-portal. Its hosted project domain becomes the stable base URL for the frontend, so choose a name your team will recognize in local and preview settings.
Add GET /v1/users/:id. The :id segment is a dynamic path input: requesting /v1/users/usr_123 resolves the route. Select 200 OK and paste a realistic static JSON profile.
{
"id": "usr_123",
"name": "Alex Johnson",
"email": "alex@acme.dev",
"role": "admin",
"active": true
}A dynamic path does not automatically personalize the response. Mockport returns the static JSON you configured.
02
Point Vite at the hosted domain
Keep the domain outside application code so the same frontend can target Mockport now and a real service later. Vite exposes variables prefixed with VITE_ to browser code.
Restart the development process after changing the environment file. Construct paths from one normalized base URL rather than scattering full URLs through components.
VITE_API_URL=https://customer-portal.mockport.devCommit an .env.example with the variable name. Keep deployment-specific values in each environment.
03
Type the contract and write fetchUser()
A small TypeScript interface turns the mock response into an explicit frontend contract. Check the HTTP status before parsing so an error payload is not mistaken for a User.
Treat 404 separately because the interface can provide a specific empty state. Other non-success statuses become ordinary failures with a message suitable for a retry panel.
export interface User {
id: string;
name: string;
email: string;
role: "admin" | "member";
active: boolean;
}
export async function fetchUser(
id: string,
signal?: AbortSignal
): Promise<User> {
const base = import.meta.env.VITE_API_URL;
if (!base) throw new Error("VITE_API_URL is missing");
const response = await fetch(
`${base.replace(/\/$/, "")}/v1/users/${encodeURIComponent(id)}`,
{ signal }
);
if (response.status === 404) throw new Error("USER_NOT_FOUND");
if (!response.ok) throw new Error("PROFILE_REQUEST_FAILED");
return response.json() as Promise<User>;
}04
Render loading, error, and profile states
Drive the request from the current id input and clear stale results before each load. That prevents an old profile from flashing while a new identifier is being fetched.
Use an explicit state union so every branch has a complete visual outcome. Cancel superseded requests or ignore their results to prevent older requests from overwriting newer selections.
import type { User } from "./user-api";
export type ViewState =
| { kind: "idle" }
| { kind: "loading" }
| { kind: "ready"; user: User }
| { kind: "error"; message: string };
// Inside your React component:
async function loadProfile(id: string) {
setState({ kind: "loading" });
try {
const user = await fetchUser(id);
setState({ kind: "ready", user });
} catch (error) {
const message =
error instanceof Error ? error.message : "UNKNOWN";
setState({ kind: "error", message });
}
}- Disable the submit action while loading.
- Announce failures in a role="alert" region.
- Keep the requested id visible so testers can reproduce the state.
05
Add a deliberate 404 route
Create GET /v1/users/not-found as a separate route with 404 Not Found and a static error body. The dedicated path makes the scenario obvious without changing the success fixture.
Enter not-found in the id control and verify that the frontend shows a useful “No profile found” state rather than a generic crash. Users should be able to correct the id or return to the list.
{
"error": "user_not_found",
"message": "No profile exists for this id."
}Verify that explicit routes take precedence over dynamic routes in your runtime.
06
Run the final test checklist
Test from the interface instead of only opening the endpoint in a browser. Prove the complete request lifecycle, including transitions between states.
Once every item passes, the frontend contract is concrete enough for backend implementation and visual review. Keep the Mockport URL available for preview builds until the real service reaches parity.
- usr_123 renders the expected name, email, role, and active state.
- Changing the id starts a fresh loading state.
- not-found renders the dedicated 404 experience.
- Other failures expose a retry action and readable alert.
- VITE_API_URL is the only place the hosted domain is configured.
Keep the contract predictable, then keep building.
You now have a real HTTPS contract, typed client code, and complete interface states without waiting on backend delivery. When the real service is ready, change the environment value and rerun the same checklist.
Put your next hosted endpoint online in minutes.
Start a 14-day trial. The $9/month Team plan includes 5 project domains, 15 routes per project, and up to 5 seats.
Start free trial →