Routes
Overview
Sometimes it's not convenient to use urls like this
https://api.jsonhub.net/username/my-collection-title
and it would be great if we could somehow assign our json collection to specific route, example
https://api.jsonhub.net/api/v1/users
Using routes we can archive this goal
Usage
Using custom routes we will be able to query json collection by using other endpoints, for example by creating custom routes we can query to users using this endpoint
curl --request GET 'https://json.jsonhub.net/api/v1/users' \
--header 'Accept: application/json'
[
{
"id": "b9c58b0f-2f7a-3112-865b-a444285ce471",
"first_name": "Merlin",
"last_name": "Hoeger",
"email": "[email protected]",
"email_verified": true,
"username": "ibrakus",
"b_day": "1990-04-12",
"ip_address": "246.152.251.210",
"created_at": {
"date": "2008-09-30 19:22:02.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "1987-09-09 20:48:09.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"last_login": {
"date": "2024-01-05 17:38:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
},
{
"id": "022f8ff7-3dfe-3193-9258-e2a6d77f2186",
"first_name": "Ignacio",
"last_name": "Kiehn",
"email": "[email protected]",
"email_verified": false,
"username": "miller84",
"b_day": "1992-11-29",
"ip_address": "109.211.94.24",
"created_at": {
"date": "1958-06-20 06:48:37.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2022-07-24 18:59:59.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"last_login": {
"date": "2023-12-31 22:24:19.000000",
"timezone_type": 3,
"timezone": "UTC"
}
},
{
"id": "1b7e5910-58b1-3b83-8639-792409984e93",
"first_name": "Robin",
"last_name": "Wiegand",
"email": "[email protected]",
"email_verified": true,
"username": "casper.uriah",
"b_day": "1987-07-04",
"ip_address": "59.173.248.223",
"created_at": {
"date": "1976-07-08 04:07:27.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "1960-01-25 18:18:53.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"last_login": {
"date": "2024-02-17 04:18:39.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
Create
To be able to create routes you must be Signed in and have one of the paid plans.
For more detail check Plans
Fill all inputs
- Write whatever route you want, example
api/v2/posts
- Attach any json collection you want using search bar, or you can assign generator (if you plan includes it)
- Select visibility (For private routes you must create Access tokens from developer menu)
- Select method (GET/POST/PATCH/PUT/DELETE)
- Select status code you would like to get (by default it's
200
)
That's it, now you can query to your newly created route
curl --request GET 'https://my-name.jsonhub.net/api/v1/posts' \
--header 'Accept: application/json'
{
"id": 1,
"title": "His mother had always taught him",
"body": "His mother had always taught him not to ever think of himself as better than others. He'd tried to live by this motto. He never looked down on those who were less fortunate or who had less money than him. But the stupidity of the group of people he was talking to made him change his mind.",
"userId": 9,
"tags": [
"history",
"american",
"crime"
],
"reactions": 2
},
Dynamic routes
Note
Dynamic routes work only for json collections. You can't use them for generators
Using dynamic routes you can retrieve a single object from collection by using an attribute
For example we have users collection
{
"data": [
{
"id": "19279cda-2547-349c-a7f4-85a5b55431f1",
"email": "[email protected]",
"firstName": "Moses",
"lastName": "Rempel"
},
{
"id": "70e6ef20-f777-3561-b72e-658147da3056",
"email": "[email protected]",
"firstName": "Rahul",
"lastName": "Kunde"
},
{
"id": "687d6560-dade-366e-b8d2-22aeb9dbba52",
"email": "[email protected]",
"firstName": "Kyra",
"lastName": "Strosin"
}
]
}
and want to get a single user data by id
or email
or use an attribute you want, e.g. we want to query to this routes and get a single user object with corresponding email address
api/v1/users/[email protected]
api/v1/users/[email protected]
With a help of dynamic routes we can have a single url, to do this we need to add a variable to the url
Example: api/v1/users/:email
Then select a pattern for your variable which will be used to find a single object from json collection. In our case it will be data.*.email
, this will search in data
object and for each (*
) find a user by email
key
Now when you make a request to username.jsonhub.net/users/[email protected]
this will return user object which has an email equal to [email protected]
{
"id": "687d6560-dade-366e-b8d2-22aeb9dbba52",
"email": "[email protected]",
"firstName": "Kyra",
"lastName": "Strosin"
}
In case you want to have different structures for a single object response you can update Single object response
input, e.g. set Single object response
filed
{
"status": "success",
"myResponseKey": "@singleResource"
}
Then by making a request again to username.jsonhub.net/users/[email protected]
we will receive data with a new structure
{
"status": "success",
"myResponseKey" :{
"id": "687d6560-dade-366e-b8d2-22aeb9dbba52",
"email": "[email protected]",
"firstName": "Kyra",
"lastName": "Strosin"
}
}