All articles

One Node.js API for Next.js and React Native

One product, two interfaces, and a contract that can survive independent releases.

A browser and smartphone connected to a shared green server hub

A customer starts an application on a laptop, adds a document from a phone, and expects both screens to show the same progress. That expectation creates a useful architecture question: which parts of the product should web and mobile actually share?

My work includes Next.js web applications and React Native mobile applications, with Node.js and PostgreSQL on the backend. Products such as ImpÔCompta make this question relevant to the kind of systems I build. Here is a reference design for a shared API serving those two clients.

The central decision is to put business rules behind a stable API while allowing each interface to fit its platform.

Start with the business action

Imagine an application with three actions: save personal details, attach a document, and submit for review. A browser can present those actions on one page. A mobile app might split them across several screens.

The API should describe the actions independently of that navigation. A route such as POST /applications/:id/submit communicates a business transition. A route named after the mobile app’s fourth screen couples the backend to a temporary interface decision.

Before implementing submission, write down its rules. Who owns the application? Which fields are required? Which document states are acceptable? Can an already submitted application be edited? Both clients should receive the same answers because the backend enforces the transition.

Share a contract with a narrow purpose

In a TypeScript codebase, a small contracts package can define request shapes, response shapes, and stable error codes. Keep database models and server configuration out of that package. A client should receive exactly the fields it needs to display, including explicitly serialized dates, rather than a database object with accidental internal fields.

For example, a response contract might look like this:

type ApplicationSummary = {
  id: string;
  status: 'draft' | 'submitted' | 'in_review';
  updatedAt: string; // ISO 8601 timestamp
  missingFields: string[];
};

This gives the two interfaces a common vocabulary. It does not validate incoming JSON. TypeScript removes type assertions during compilation, so casting a request body does not establish that it has the expected shape. Validate untrusted input at runtime before applying business rules. The distinction is documented in the TypeScript handbook (opens in a new tab).

Keep validation errors useful to both clients. A stable code such as APPLICATION_INCOMPLETE, accompanied by field identifiers, lets the website highlight inputs while the mobile app routes someone back to the relevant step. Human-readable text can then follow each interface’s language and accessibility requirements.

Authorize the resource on every request

An application ID identifies a record; it does not grant permission to change it. Resolve the authenticated user, then check that user’s access to the requested application before reading or modifying it. Repeat the check when attaching a document, generating an upload URL, and submitting the application.

Web and mobile can use different session mechanisms while sharing this authorization logic. For a browser cookie session, consider cookie attributes and CSRF protection as part of the design. For mobile, use an appropriate credential storage mechanism and an explicit expiration and refresh policy. Avoid making a shared contract package responsible for storing credentials.

I would also keep administrative fields out of customer-facing update requests. Accepting a field in JSON should never be enough to make it writable.

Give retries a deliberate meaning

Consider this sequence: the server commits a submission, the network drops before the response arrives, and the customer taps Submit again. The second request needs a defined outcome.

A useful design accepts an idempotency key for that operation. Scope the key to the authenticated actor and operation, store a fingerprint of the request, and persist the result alongside the relevant database change. Reusing the key with different input should produce a conflict. A concurrent duplicate should wait for, or retrieve, the original result rather than start a second operation.

Document how long keys remain valid and what happens while a request is still in progress. Disabling the submit button improves the interface, but the server still needs to handle repeated requests from retries, old clients, and multiple devices.

Treat installed mobile versions as active consumers

A website deployment can replace its frontend quickly. An installed mobile app can continue using an older contract. Before changing a response, ask what that older client will do.

Prefer additive changes when clients tolerate them. Define behavior for unknown enum values. When a breaking change is necessary, provide a migration window and a clear compatibility policy. Removing a field is a product release decision as well as a code change.

Contract fixtures can make this concrete. Keep a set of responses representing normal, incomplete, unauthorized, and conflict states, and verify both clients against them. Add a test that repeats the same submission and checks that only one business operation exists afterward.

Keep the first implementation small

Start with one complete flow: create a draft, save a step, submit it, and reload its status from the other client. Include authorization failures and a dropped response in that first slice.

Once that flow behaves consistently, the shared API has proved something useful: the product remains coherent even when its interfaces take different paths. That is the foundation on which the next screen, client, and release can build.

Keep reading

React Native Onboarding That Survives an Interrupted Session

Contact

Let’s connect.

For engineering opportunities, reach me by email or LinkedIn.