Prerequisites
To follow this tutorial, you should understand APIs, HTTP requests (GET, POST), and a tool to test APIs like Postman, Insomnia, or cURL.
Steps for API Testing
1- Create a Test Project
First, create a new project specifically for testing purposes. For example, we'll name our project "API-Test."
2- Copy the Project ID
Next, locate and copy the project ID. The project ID can typically be found in the URL when you are viewing the project details.
Example URL: ?module=projekt&action=dashboard&id=21
In this case, the project ID is 21.
3- Use the Test Project ID for POST Requests
When making POST requests to create new entries, make sure to use the test project ID in the request. This will ensure that the entries are created in the test environment, not the production environment.
Example POST request:
POST {{ _.base_url }}/api/products
Accept: application/problem+json
Content-Type: application/vnd.xentral.default.v1+json
Authorization: Bearer YOUR_API_KEY
{
"name": "Product created through API",
"description": "test description",
"shortDescription": "test short description",
"project": {
"id": "21"
}
}
4- Filter GET Requests by Project ID
When you want to retrieve entries related to your test project, you can filter the results using the test project ID in the GET request.
Example GET request:
GET {{ _.base_url }}/api/products?filter[0][key]=project&filter[0][op]=equals&filter[0][value]=12345
Accept: application/problem+json
Content-Type: application/vnd.xentral.default.v1+json
Authorization: Bearer YOUR_API_KEY
Benefits of Using a Test Project
Using a separate test project for API testing has several advantages:
- Isolation from the production environment reducing the risk of accidental changes or data corruption.
- A controlled environment to test new features, bug fixes, or API changes.
- Easier identification and cleanup of test data.
Future Enhancements
In the future, we plan to add more granular access control to API tokens, preventing users from accidentally writing data (such as sales orders) into the production environment. This will help maintain data integrity, as some data types, like sales orders, cannot be deleted.
By following this tutorial, you can efficiently test APIs in a safe and controlled environment. This will help you catch potential issues before they impact the production environment and ensure a smooth user experience.