When building REST APIs with Express.js, we usually work with several HTTP methods:
GET
POST
PUT
PATCH
DELETE
Each method has a different purpose.
So far, we've looked at:
- GET — retrieve data
- POST — create new data
Now let's look at two methods used for updating existing data:
PUT
PATCH
At first glance, they might seem identical.
For example:
PUT /api/users/10
PATCH /api/users/10
Both appear to update user 10.
But there is an important difference between them.
Understanding that difference will help you design cleaner REST APIs and avoid common API design mistakes.
In this article, we'll learn:
- What PUT is
- What PATCH is
- How to create PUT APIs in Express.js
- How to create PATCH APIs
- The difference between PUT and PATCH
- How
req.paramsandreq.bodywork with update APIs - Validation
- Status codes
- Real-world examples
- Common mistakes
- When to use PUT and PATCH
Let's start.
What Is PUT?
PUT is an HTTP method commonly used to replace or completely update an existing resource.
For example:
PUT /api/users/10
This means:
Update user 10 with the provided representation.
Suppose the existing user is:
{
"id": 10,
"name": "John",
"email": "john@example.com",
"age": 25
}
A PUT request might send:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
The server can treat this as the new representation of the user.
A simple mental model is:
PUT
↓
Replace the resource
What Is PATCH?
PATCH is an HTTP method used for partial updates.
Instead of sending the complete resource, you send only the fields that need to change.
For example:
PATCH /api/users/10
Request body:
{
"name": "John Smith"
}
Only the name needs to change.
The existing data:
{
"id": 10,
"name": "John",
"email": "john@example.com",
"age": 25
}
becomes:
{
"id": 10,
"name": "John Smith",
"email": "john@example.com",
"age": 25
}
The other fields remain unchanged.
A simple mental model:
PATCH
↓
Change part of the resource
PUT vs PATCH in Simple Terms
This is the easiest way to remember the difference:
PUT
"Here is the new version of this resource."
PATCH
"Change these specific fields of this resource."
For example, imagine a user:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 25
}
With PUT:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
You're sending the complete representation.
With PATCH:
{
"name": "John Smith"
}
You're changing only the name.
Creating Our Express Project
Let's create a small Express application.
mkdir express-update-api
cd express-update-api
npm init -y
npm install express
Create:
server.js
We'll use an in-memory array instead of a database so that we can focus on PUT and PATCH.
Step 1: Create the Express Server
const express = require("express");
const app = express();
const PORT = 3000;
app.use(express.json());
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
The important middleware is:
app.use(express.json());
This allows Express to parse JSON request bodies.
Step 2: Create Sample Users
Let's create some sample data:
const users = [
{
id: 1,
name: "John",
email: "john@example.com",
age: 25
},
{
id: 2,
name: "Jane",
email: "jane@example.com",
age: 28
}
];
We'll update these users using PUT and PATCH.
Before Updating, Understand the URL
Our update endpoint will be:
/api/users/:id
For example:
/api/users/1
Here:
1
is the route parameter.
Express allows us to access it using:
req.params.id
The data we want to update comes through:
req.body
So an update request generally looks like:
PUT /api/users/1
↓
req.params.id
Request Body
↓
req.body
This is an important pattern to remember.
Creating a PUT API
Let's create our first PUT route.
app.put("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const { name, email, age } = req.body;
console.log(userId);
console.log(name);
console.log(email);
console.log(age);
res.json({
message: "User updated successfully"
});
});
Now send:
PUT /api/users/1
with:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
Inside Express:
req.params.id
is:
1
and:
req.body
contains:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
Updating the User
Now let's actually update our array.
app.put("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const userIndex = users.findIndex(
(user) => user.id === userId
);
if (userIndex === -1) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
users[userIndex] = {
id: userId,
name,
email,
age
};
res.json({
message: "User updated successfully",
user: users[userIndex]
});
});
Now let's understand what's happening.
Step-by-Step PUT Flow
Suppose we send:
PUT /api/users/1
with:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
Step 1: Get the ID
const userId = Number(req.params.id);
Result:
1
Step 2: Find the user
const userIndex = users.findIndex(
(user) => user.id === userId
);
Step 3: Check whether the user exists
if (userIndex === -1) {
return res.status(404).json({
message: "User not found"
});
}
Step 4: Read the request body
const { name, email, age } = req.body;
Step 5: Replace the resource
users[userIndex] = {
id: userId,
name,
email,
age
};
Step 6: Send the response
res.json({
message: "User updated successfully",
user: users[userIndex]
});
What Happens If a PUT Field Is Missing?
This is where PUT and PATCH become important.
Suppose the existing user is:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 25
}
Now the client sends:
PUT /api/users/1
with only:
{
"name": "John Smith"
}
Our previous implementation does:
users[userIndex] = {
id: userId,
name,
email,
age
};
Since email and age weren't provided, they become:
{
"id": 1,
"name": "John Smith"
}
The old values are lost.
That's why PUT is generally treated as a complete replacement operation.
If your API contract says PUT requires the full resource, you should validate all required fields.
For example:
if (!name || !email || age === undefined) {
return res.status(400).json({
message: "Name, email and age are required"
});
}
This makes the API behavior clear.
Creating a PATCH API
Now let's create a PATCH route.
app.patch("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const user = users.find(
(user) => user.id === userId
);
if (!user) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
if (name !== undefined) {
user.name = name;
}
if (email !== undefined) {
user.email = email;
}
if (age !== undefined) {
user.age = age;
}
res.json({
message: "User updated successfully",
user
});
});
Now suppose the existing user is:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 25
}
Send:
PATCH /api/users/1
with:
{
"name": "John Smith"
}
The result becomes:
{
"id": 1,
"name": "John Smith",
"email": "john@example.com",
"age": 25
}
Only the name changed.
Why Use !== undefined?
You might wonder why we wrote:
if (name !== undefined) {
user.name = name;
}
instead of:
if (name) {
user.name = name;
}
The difference matters.
Suppose someone wants to update a value to:
""
or:
0
These values are falsy in JavaScript.
Using:
if (age)
would incorrectly ignore:
{
"age": 0
}
Using:
if (age !== undefined)
means:
Update the field if the client actually sent it.
That's much safer for PATCH logic.
PUT Example
Imagine the current resource:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 25
}
PUT request:
PUT /api/users/1
Body:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
Result:
{
"id": 1,
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 26
}
The resource is replaced with the submitted representation.
PATCH Example
Current resource:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 25
}
PATCH request:
PATCH /api/users/1
Body:
{
"age": 26
}
Result:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"age": 26
}
Only age changed.
PUT vs PATCH: The Core Difference
Here's the most important concept:
PUT
→ Replace the resource
PATCH
→ Partially modify the resource
Think about updating a profile.
If the frontend sends the entire profile:
{
"name": "John",
"email": "john@example.com",
"age": 26,
"city": "Hyderabad"
}
PUT is a natural fit.
If the frontend only wants to change the city:
{
"city": "Bengaluru"
}
PATCH is a natural fit.
PUT and PATCH URLs Can Be the Same
There's nothing wrong with having:
PUT /api/users/1
and:
PATCH /api/users/1
The URL identifies the resource.
The HTTP method tells the server what kind of operation we're performing.
For example:
GET /api/users/1
→ Get user
PUT /api/users/1
→ Replace/update user
PATCH /api/users/1
→ Partially update user
DELETE /api/users/1
→ Delete user
This is a clean REST API design.
PUT vs PATCH with a Real Example
Imagine a blog post:
{
"id": 10,
"title": "Introduction to Node.js",
"content": "Node.js is a JavaScript runtime...",
"author": "John",
"published": true
}
If you're replacing the entire blog:
PUT /api/blogs/10
Body:
{
"title": "Introduction to Node.js",
"content": "Updated content...",
"author": "John",
"published": true
}
But if you only want to publish the blog:
PATCH /api/blogs/10
Body:
{
"published": true
}
PATCH makes the intention much clearer.
Updating Nested Objects
Real applications often contain nested data.
For example:
{
"id": 1,
"name": "John",
"address": {
"city": "Hyderabad",
"country": "India"
}
}
With PATCH:
PATCH /api/users/1
you could send:
{
"address": {
"city": "Bengaluru"
}
}
However, be careful with how you implement nested updates.
A naive replacement:
user.address = req.body.address;
would remove:
country
if only city was provided.
For nested updates, you need explicit merge/update logic.
For example:
if (req.body.address) {
user.address = {
...user.address,
...req.body.address
};
}
Now:
{
"city": "Bengaluru"
}
updates the city while preserving the country.
This becomes particularly important when working with MongoDB or other databases.
PATCH Can Update One Field
One of the most useful things about PATCH is that you can update a single property.
For example:
PATCH /api/users/1
Body:
{
"email": "newemail@example.com"
}
Or:
{
"age": 30
}
Or:
{
"name": "David"
}
This is useful for UI interactions.
For example, suppose you have an admin dashboard with:
User Status: Active
When an admin clicks a button, you might send:
PATCH /api/users/1
{
"status": "inactive"
}
There's no reason to send the entire user object.
PATCH and Boolean Values
Be careful with boolean fields.
Suppose:
{
"isActive": true
}
You want to change it to:
{
"isActive": false
}
Don't write:
if (isActive) {
user.isActive = isActive;
}
because false is a falsy value.
Instead:
if (isActive !== undefined) {
user.isActive = isActive;
}
This is an important pattern for PATCH APIs.
Validating PUT Requests
Because PUT generally represents a complete replacement, validation is important.
For example:
app.put("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const userIndex = users.findIndex(
(user) => user.id === userId
);
if (userIndex === -1) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
if (!name || !email || age === undefined) {
return res.status(400).json({
message: "Name, email and age are required"
});
}
if (typeof age !== "number") {
return res.status(400).json({
message: "Age must be a number"
});
}
users[userIndex] = {
id: userId,
name,
email,
age
};
res.json({
message: "User replaced successfully",
user: users[userIndex]
});
});
Now the API expects all required fields.
Validating PATCH Requests
PATCH is different because fields are optional.
Suppose:
PATCH /api/users/1
with:
{
"age": 30
}
We shouldn't require:
name
email
because the purpose of PATCH is to update only selected fields.
Instead:
app.patch("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const user = users.find(
(user) => user.id === userId
);
if (!user) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
if (name !== undefined) {
if (typeof name !== "string" || !name.trim()) {
return res.status(400).json({
message: "Name must be a valid string"
});
}
user.name = name;
}
if (email !== undefined) {
if (typeof email !== "string" || !email.includes("@")) {
return res.status(400).json({
message: "Invalid email"
});
}
user.email = email;
}
if (age !== undefined) {
if (typeof age !== "number") {
return res.status(400).json({
message: "Age must be a number"
});
}
user.age = age;
}
res.json({
message: "User updated successfully",
user
});
});
This is more appropriate for a partial update.
What Status Code Should PUT Return?
A successful update can commonly return:
200 OK
For example:
res.status(200).json({
message: "User updated successfully",
user
});
If your API successfully performs an update but doesn't return a response body, 204 No Content can also be appropriate.
For example:
res.status(204).send();
The important thing is to keep the API behavior consistent.
What Status Code Should PATCH Return?
Similarly, a successful PATCH can commonly return:
200 OK
with the updated resource:
res.status(200).json({
message: "User updated successfully",
user
});
You can also use:
204 No Content
if you don't want to return a response body.
What If the Resource Doesn't Exist?
Suppose the client sends:
PATCH /api/users/999
but user 999 doesn't exist.
The API should not silently succeed.
Return:
404 Not Found
For example:
if (!user) {
return res.status(404).json({
message: "User not found"
});
}
This gives the client a clear indication that the requested resource doesn't exist.
PUT vs PATCH and Idempotency
There's another concept worth knowing: idempotency.
You don't need to memorize the word immediately, but understanding the idea is useful.
An operation is idempotent when performing the same operation multiple times has the same intended final result as performing it once.
For example:
PUT /api/users/1
with:
{
"name": "John",
"email": "john@example.com",
"age": 30
}
If you send the same PUT request multiple times, the user should still end up with:
{
"name": "John",
"email": "john@example.com",
"age": 30
}
PATCH is often designed to be idempotent too, depending on the specific patch operation.
For example:
{
"name": "John Smith"
}
repeatedly results in the same name.
But a PATCH operation such as:
{
"incrementAge": 1
}
would have different semantics if repeated.
The key point is that idempotency depends on what the operation actually does, not simply on the HTTP method name.
Complete Example
Let's combine our PUT and PATCH APIs.
const express = require("express");
const app = express();
const PORT = 3000;
app.use(express.json());
const users = [
{
id: 1,
name: "John",
email: "john@example.com",
age: 25
},
{
id: 2,
name: "Jane",
email: "jane@example.com",
age: 28
}
];
// PUT - Replace entire user
app.put("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const userIndex = users.findIndex(
(user) => user.id === userId
);
if (userIndex === -1) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
// PUT expects the complete resource
if (!name || !email || age === undefined) {
return res.status(400).json({
message: "Name, email and age are required"
});
}
if (typeof age !== "number") {
return res.status(400).json({
message: "Age must be a number"
});
}
users[userIndex] = {
id: userId,
name,
email,
age
};
res.status(200).json({
message: "User replaced successfully",
user: users[userIndex]
});
});
// PATCH - Partially update user
app.patch("/api/users/:id", (req, res) => {
const userId = Number(req.params.id);
const user = users.find(
(user) => user.id === userId
);
if (!user) {
return res.status(404).json({
message: "User not found"
});
}
const { name, email, age } = req.body;
if (name !== undefined) {
user.name = name;
}
if (email !== undefined) {
user.email = email;
}
if (age !== undefined) {
if (typeof age !== "number") {
return res.status(400).json({
message: "Age must be a number"
});
}
user.age = age;
}
res.status(200).json({
message: "User updated successfully",
user
});
});
app.listen(PORT, () => {
console.log(
`Server running on http://localhost:${PORT}`
);
});
Testing the PUT API
Request
PUT http://localhost:3000/api/users/1
Body:
{
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 30
}
Response
{
"message": "User replaced successfully",
"user": {
"id": 1,
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 30
}
}
Testing the PATCH API
Request
PATCH http://localhost:3000/api/users/1
Body:
{
"age": 31
}
Response
{
"message": "User updated successfully",
"user": {
"id": 1,
"name": "John Smith",
"email": "johnsmith@example.com",
"age": 31
}
}
Notice that we didn't send:
name
email
They remained unchanged.
Common Mistakes
Mistake 1: Treating PUT and PATCH as Exactly the Same
You might see:
app.put("/api/users/:id", updateUser);
app.patch("/api/users/:id", updateUser);
There is nothing technically stopping you from doing this.
But you're losing the semantic difference between the methods.
If your API supports both, define clearly what each one means.
Mistake 2: Using PATCH but Replacing the Entire Object
For example:
app.patch("/api/users/:id", (req, res) => {
users[userIndex] = req.body;
});
Suppose the request body is:
{
"name": "John Smith"
}
You could accidentally remove:
email
age
id
That's not what most clients expect from a partial update.
Instead, update only fields that were supplied.
Mistake 3: Using Truthy Checks for PATCH
Avoid:
if (age) {
user.age = age;
}
because:
0
false
""
are falsy.
Prefer:
if (age !== undefined) {
user.age = age;
}
Mistake 4: Not Validating PUT Data
If PUT represents a complete replacement, don't accept an incomplete body without thinking about the consequences.
For example:
{
"name": "John"
}
might unintentionally remove other fields.
Validate the required representation.
Mistake 5: Not Checking Whether the Resource Exists
Always check:
const user = users.find(...);
before updating.
If it doesn't exist:
404 Not Found
is generally appropriate.
When Should You Use PUT?
Use PUT when the client wants to replace the resource or your API contract defines PUT as a full update.
For example:
PUT /api/users/10
Body:
{
"name": "John",
"email": "john@example.com",
"age": 30,
"city": "Hyderabad"
}
This works well when the frontend has the complete current representation.
When Should You Use PATCH?
Use PATCH when the client only wants to change specific fields.
For example:
PATCH /api/users/10
Body:
{
"city": "Bengaluru"
}
Or:
{
"isActive": false
}
Or:
{
"email": "newemail@example.com"
}
This is particularly useful for dashboards, settings pages, profile updates, status changes, and other partial modifications.
A Real-World API Example
Imagine an e-commerce application.
You might have:
GET /api/products
GET /api/products/:id
POST /api/products
PUT /api/products/:id
PATCH /api/products/:id
DELETE /api/products/:id
For example:
Get product
GET /api/products/101
Create product
POST /api/products
Replace product
PUT /api/products/101
Change product price
PATCH /api/products/101
Body:
{
"price": 799
}
Delete product
DELETE /api/products/101
This is a common REST-style API structure.
PUT and PATCH in a Frontend Application
Suppose you have a React form.
The form contains:
Name
Email
Age
When the user submits the entire form, you might use:
fetch("/api/users/1", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "John Smith",
email: "john@example.com",
age: 30
})
});
But suppose the UI only has an "Enable Account" button.
You don't need to send the entire user:
fetch("/api/users/1", {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
isActive: true
})
});
That's one of the practical benefits of PATCH.
The Bigger REST API Picture
Now we have covered the main CRUD operations:
CREATE
↓
POST
READ
↓
GET
UPDATE
↓
PUT / PATCH
DELETE
↓
DELETE
A typical users API might look like:
GET /api/users
GET /api/users/:id
POST /api/users
PUT /api/users/:id
PATCH /api/users/:id
DELETE /api/users/:id
This gives us a complete CRUD API.
Final Thoughts
PUT and PATCH both update resources, but they communicate different intentions.
The simplest way to remember them is:
PUT
→ Replace the resource
PATCH
→ Change part of the resource
In Express.js, the syntax is straightforward:
app.put("/api/users/:id", (req, res) => {
// Replace resource
});
and:
app.patch("/api/users/:id", (req, res) => {
// Partially update resource
});
Both commonly use:
req.params.id
to identify the resource and:
req.body
to receive the update data.
The complete flow looks like this:
Client
↓
PUT / PATCH
↓
Route Parameter
↓
req.params
↓
Request Body
↓
req.body
↓
Validation
↓
Update Resource
↓
Response
Once you understand PUT and PATCH, you have covered another major part of REST API development.
The next natural step is DELETE, where we'll learn how to remove resources safely and how status codes such as 204 No Content are commonly used.