Connect with OneHash via Rest APIs
REST API
OneHash generates REST API for all of your DocTypes out of the box and can be used to seemless integration with other systems.
Authentication
There are two ways to authenticate through REST API. Token based authentication and password based authentication.
1. Token Based Authentication
A token is a pair of API Key and API Secret. First, you must create an API User and then generate the keys in the API Access section in the User form.
The token is generated by concatenating api_key and api_secret with a colon : Pass the string token api_key:api_secret to the Authorization header in the request.
fetch ("http://<base-url>/api/resource/:doctype', {
headers: {
"Authorization" : ‘token api_key:api_secret'
}
})
.then(r => r.json())
.then(r => {
console. log (x) ;
})
2. Password Based Authentication
Password based authentication relies on cookies and session data to maintain authentication in subsequent requests. In most cases, the library you are using to issue REST calls will handle session data, but if it doesn't you should use Token based authentication.
fetch ('http://<base-url>/api/method/login', {
method: 'POST',
headers: {
'Accept': ‘application/json',
'ContentType': 'application/json',
},
body: JSON. stringify({
usr: ‘username or email’,
pwd: 'password'
})
})
.then(r => r.json())
.then(r => {
console. log (x) ;
})
Listing Documents
To get a list of records of a DocType, send a GET request at /api/resource/:doctype. By
default, it will return 20 records and will only fetch the name of the records.
GET /api/resource/:doctype
Response
"data": [
{
"name": "b57566801a"
},
{
"name": "bé3b71d£37"
},
{
"name":"lfeadafora"
},
"name": "4cadccée0e"
},
]
}
You can specify which fields to fetch in the fields param. It should be a JSON array.
GET /api/resource/:doctype?fields=["field1", "field2"]
You can filter the records by passing filters param. Filters should be an array, where each
filter is of the format: [field, operator, value]
GET /api/resource/:doctype?filters=[["field1", "=", "value1"], ["field2",">", "value2"]]
You can also provide the sort field and order. It should be of the format fieldname asc or
fieldname desc. The space should be URL encoded.
GET /api/resource/:doctype?order_by=title%20desc
You can also page the results by providing the limit_start and limit_page_length params.
GET /api/resource/:doctype?limit_start=10&limit_page_length=20
CRUD Operations
OneHash generates REST endpoints for CRUD operations for all DocTypes automatically. Make
sure you set the following headers in your requests so that you get proper JSON responses.
{
"Accept": "application/json",
"Content-Type": "application/json",
}
Create
Create a new document by sending a POST request to /api/resource/:doctype. Send the
document as JSON in the Request Body.
POST /api/resource/:doctype
# Body
{
"description": "New ToDo"
}
Response
{
"data": {
"name": "af2e2d0e33",
"owner": "Administrator",
"creation": "2019-06-03 14:19:00.281026",
"modified": "2019-06-03 14:19:00.281026",
"modified_by": "Administrator",
"idx": 0,
"docstatus": 0,
"status": "Open",
"priority": "Medium",
"description": "New TODO",
"doctype": "ToDo"
}
}
Read
Get a document by sending a GET request to /api/resource/:doctype/:name.
GET /api/resource/:doctype/:name
Response
{
"data": {
"name": "bf2e760e13",
"Owner": "Administrator",
"creation": "2019-06-03 14:19:00.281026",
"modified": "2019-06-03 14:19:00.281026",
"modified by": "Administrator",
"idx": 0
"docstatus": 0,
"status": "Open",
"priority": "Medium",
"description": "<p>Test description</p>",
"doctype": "ToDo"
}
}
**Update**
Update a document by sending a PUT request to /api/resource/:doctype/:name. You don't need to send the whole document, instead you can just send the fields that you want to update.
PUT /api/resource/:doctype/:name
Body
{"description": "New description"}
```
Response
Delete
Delete a document by sending a DELETE request to /api/resource/:doctype/:name.
DELETE /api/resource/:doctype/:name
Response
{"message": "ok"}
File Uploads
There is a dedicated method /api/method/upload_file that accepts binary file data and uploads it into the system.
Here is the curl command for it:
If you are using client side Javascript to upload files, you can append the uploaded files as FormData and send an XHR request.
API Documentation:
API Documentation
API Playground
Updated on: 22/03/2023
Thank you!