How to use it
fakeApiData can be used with any type of shopping project that needs products, carts, and users in JSON format. you can use examples below to check how fakeApiData works and feel free to enjoy it in your awesome projects!
Products
Get all products
fetch('https://fakeapidata.com/secure/products',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
},
/*...*/
{
id:30,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
]
Get a single product
fetch('https://fakeapidata.com/secure/products/1',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
{
id:1,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
Limit results
fetch('https://fakeapidata.com/secure/products?limit=5',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
/*...*/
{
id:5,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
]
Limit with pagination
fetch('https://fakeapidata.com/secure/products?page=1&limit=2',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
/*...*/
{
id:5,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
]
Sort results
fetch('https://fakeapidata.com/secure/products?sort=desc',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
Default value is in ascending mode, you can use with 'desc' or 'asc' as you want.
//output
[
{
id:30,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
/*...*/
{
id:1,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
]
Get all categories
fetch('https://fakeapidata.com/secure/products/categories',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
"electronics",
"jewelery",
"men's clothing",
"women's clothing"
]
Get products in a specific category
fetch('https://fakeapidata.com/secure/products/category/jewelery',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
You can also use limit(Number) and sort(asc|desc) as a query string to get your ideal results
//output
[
{
id:5,
title:'...',
price:'...',
category:'jewelery',
description:'...',
image:'...'
}
/*...*/
{
id:8,
title:'...',
price:'...',
category:'jewelery',
description:'...',
image:'...'
}
]
Add new product
fetch('https://fakeapidata.com/secure/products',{
method:"POST",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
If you send an object like the code above, it will return you an object with a new id. remember that nothing in real will insert into the database. so if you want to access the new id you will get a 404 error.
//output
{
id:31,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
Update a product
fetch('https://fakeapidata.com/secure/products/7',{
method:"PUT",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
fetch('https://fakeapidata.com/secure/products/7',{
method:"PATCH",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
It will return you an object with sent id. remember that nothing in real will update in the database.
//output
{
id:7,
title:'new title',
price:'new price',
category:'new category',
description:'new description',
image:'new image url'
}
Delete a product
fetch('https://fakeapidata.com/secure/products/6',{
method:"DELETE",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'}
})
.then(res=>res.json())
.then(json=>console.log(json))
The product will not be deleted on the database. but if you sent data successfully it will return you the fake deleted product.
//output
{
id:6,
title:'...',
price:'...',
category:'...',
description:'...',
image:'...'
}
Cart
Get all carts
fetch('https://fakeapidata.com/secure/carts',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
userId:1,
date:2020-10-10,
products:[{productId:2,quantity:4},{productId:1,quantity:10},{productId:5,quantity:2}]
},
/*...*/
{
id:20,
userId:10,
date:2019-10-10,
products:[{productId:1,quantity:5},{productId:5,quantity:1}]
}
]
Get a single cart
fetch('https://fakeapidata.com/secure/carts/5',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
{
id:5,
userId:1,
date:...,
products:[...]
}
Limit results
fetch('https://fakeapidata.com/secure/carts?limit=5',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
userId:1,
date:...,
products:[...]
},
/*...*/
{
id:5,
userId:1,
date:...,
products:[...]
}
]
Limit with pagination
fetch('https://fakeapidata.com/secure/carts?page=1&limit=2',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
userId:1,
date:...,
products:[...]
},
/*...*/
{
id:5,
userId:1,
date:...,
products:[...]
}
]
Sort results
fetch('https://fakeapidata.com/secure/carts?sort=desc',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
The default value is in ascending mode, you can use it with 'desc' or 'asc' as you want.
//output
[
{
id:20,
userId:1,
date:...,
products:[...]
},
/*...*/
{
id:1,
userId:1,
date:...,
products:[...]
}
]
Get carts in a date range
fetch('https://fakeapidata.com/secure/carts/startdate=2019-12-10&enddate=2020-10-10',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
If you don't add any start date it will fetch from the beginning of time and if you don't add any end date it will fetch until now. You can also use limit(Number) and sort(asc|desc) as a query string to get your ideal results
//output
[
{
id:1,
userId:1,
date:2019-12-10,
products:[...]
},
/*...*/
{
id:4,
userId:1,
date:2020-10-10,
products:[...]
}
]
Get user carts
fetch('https://fakeapidata.com/secure/carts/user/2',
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
)
.then(res=>res.json())
.then(json=>console.log(json))
You can also use date range as query string to get your ideal results
//output
[
{
id:5,
userId:2,
date:2019-10-03,
products:[...]
},
/*...*/
{
id:6,
userId:2,
date:2020-10-10,
products:[...]
}
]
Add a new product
fetch('https://fakeapidata.com/secure/carts',{
method:"POST",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
userId:5,
date:2020-02-03,
products:[{productId:5,quantity:1},{productId:1,quantity:5}]
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
If you send an object like the code above, it will return you an object with a new id. remember that will insert into the database.It will create a new object and save till 24 hrs.
//output
{
id:21
userId:5,
date:2020-02-03,
products:[{productId:5,quantity:1},{productId:1,quantity:5}]
}
Update a product
fetch('https://fakeapidata.com/secure/carts/7',{
method:"PUT",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
userId:3,
date:2019-12-10,
products:[{productId:1,quantity:3}]
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
fetch('https://fakeapidata.com/secure/carts/7',{
method:"PATCH",,
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
body:JSON.stringify(
{
userId:3,
date:2019-12-10,
products:[{productId:1,quantity:3}]
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
It will return you an object with sent id. remember that nothing in real will update in the database.
//output
{
id:7,
userId:3,
date:2019-12-10,
products:[{productId:1,quantity:3}]
}
Delete a cart
fetch('https://fakeapidata.com/secure/carts/6',{
method:"DELETE",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'}
})
.then(res=>res.json())
.then(json=>console.log(json))
The cart will not be deleted on the database. but if you sent data successfully it will return you the fake deleted cart.
//output
{
id:6,
userId:...,
date:...,
products:[...]
}
Users
Get all users
fetch('https://fakeapidata.com/secure/users')
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
},
/*...*/
{
id:20,
email:'...',
password:'...',
name:{
firstname:'...',
lastname:'...'
},
address:{
city:'...',
street:'...',
number:...,
zipcode:'...',
geolocation:{
lat:'...',
long:'...'
}
},
phone:'...'
}
]
Get a single user
fetch('https://fakeapidata.com/secure/users/1')
.then(res=>res.json())
.then(json=>console.log(json))
//output
{
id:1,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
Limit results
fetch('https://fakeapidata.com/secure/users?limit=5')
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
},
/*...*/
{
id:5,
email:'...',
password:'...',
name:{
firstname:'...',
lastname:'...'
},
address:{
city:'...',
street:'...',
number:...,
zipcode:'...',
geolocation:{
lat:'...',
long:'...'
}
},
phone:'...'
}
]
Limit with pagination
fetch('https://fakeapidata.com/secure/users?page=1&limit=2')
.then(res=>res.json())
.then(json=>console.log(json))
//output
[
{
id:1,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
},
/*...*/
{
id:5,
email:'...',
password:'...',
name:{
firstname:'...',
lastname:'...'
},
address:{
city:'...',
street:'...',
number:...,
zipcode:'...',
geolocation:{
lat:'...',
long:'...'
}
},
phone:'...'
}
]
Sort results
fetch('https://fakeapidata.com/secure/users?sort=desc')
.then(res=>res.json())
.then(json=>console.log(json))
The default value is in ascending mode, you can use it with 'desc' or 'asc' as you want.
//output
[
{
id:20,
email:'...',
password:'...',
name:{
firstname:'...',
lastname:'...'
},
address:{
city:'...',
street:'...',
number:...,
zipcode:'...',
geolocation:{
lat:'...',
long:'...'
}
},
phone:'...'
},
/*...*/
{
id:1,
email:'...',
password:'...',
name:{
firstname:'...',
lastname:'...'
},
address:{
city:'...',
street:'...',
number:...,
zipcode:'...',
geolocation:{
lat:'...',
long:'...'
}
},
phone:'...'
}
]
Add a new user(please provide valid email id)
fetch('https://fakeapidata.com/secure/users',{
method:"POST" ,
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
If you send an object like the code above, it will return you an object with a new id. remember that nothing in real will insert into the database. so if you want to access the new id you will get a 404 error.
//output
{
id:21,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
Update a users
fetch('https://fakeapidata.com/secure/users/7',{
method:"PUT" ,
headers:{'Content-Type':'application/json','Authorization':'Bearer token'},
body:JSON.stringify(
{
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
fetch('https://fakeapidata.com/secure/users/7',{
method:"PATCH",
,
{ headers:{'Content-Type':'application/json','Authorization':'Bearer token'}}
body:JSON.stringify(
{
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
It will return you an object with sent id. remember that nothing in real will update in the database.
//output
{
id:7,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
Delete a user
fetch('https://fakeapidata.com/secure/users/6',{
method:"DELETE",
headers:{'Content-Type':'application/json','Authorization':'Bearer token'}
})
.then(res=>res.json())
.then(json=>console.log(json))
The user will not be deleted on the database. but if you sent data successfully it will return you the fake deleted user.
//output
{
id:6,
email:'[email protected]',
password:'m38rmF$',
name:{
firstname:'John',
lastname:'Doe'
},
address:{
city:'kilcoole',
street:'7835 new road',
number:3,
zipcode:'12926-3874',
geolocation:{
lat:'-37.3159',
long:'81.1496'
}
},
phone:'1-570-236-7033'
}
Login
User login
fetch('https://fakeapidata.com/auth/login',{
method:'POST',
headers:{'Content-Type':'application/type'},
body:JSON.stringify({
email: "[email protected]",
password: "83r5^_",
expiresin: 5( it is an optional param , provides token validity for these many mins)
})
})
.then(res=>res.json())
.then(json=>console.log(json))
//output
{
token: "eyJhbGciOiJIUzI1NiIsInR"
}
You can use any of the users' email and password available in users API to get token. any other email return error.