Guide
API
Overview.
In the world of Automate Everything, it is not just a nice to have thing but a requirement to expose broad and well-defined API.
Here, at Perforator, we follow an API-first approach.
So, all the functionality of our platform, like WEB-UI and multiple performance testing integrations - everything is powered by the same API.
Our API is defined in
"Open API Specification"
(formerly Swagger)
format, and you can access it as both YAML and JSON:
- https://api.perforator.io/oas.yml
- https://api.perforator.io/oas.json
We have integrated swagger-ui, so you can start playing with it directly from your browser:
https://api.perforator.io/swagger-ui/
openapi: 3.0.1
info:
title: Perforator API
contact:
name: Perforator Inc.
url: 'https://www.perforator.io'
email: support@perforator.io
version: v1
servers:
- url: 'https://api.perforator.io'
security:
- OAuth: []
#...
Security.
Access to Perforator API requires all API calls to be authorized, meaning that you should register first on the platform and get your unique credentials. Thus, secured API access is divided into two standalone phases: authentication and authorization.
During the authentication phase, you need to provide your own set of client_id / client_secret and exchange it to the JWT access token using OAuth 2.0 Client Credentials Grant flow. Please keep in mind that client_id and client_secret are not the same as your login/password to access the web application.
Please open the "Settings / API" page to get your API credentials.
API Client ID identifies your organization, so all users within the same organization share the same id - such key can be easily shared with your colleagues and doesn't require a secure vault for storage.
API Client Secret is your security key to confirm that you are really you during API authentication calls. You can create multiple secret keys according to your requirements or projects need. Please keep it in a secret and safe place since anybody who knows your secret key can act on your behalf. The general rule is to treat the secret key the same way as your password. We store it in a hashed and salted way without keeping its plain text value on our servers. As a result, your secret key is only displayed once at the time of its generation, and can't be revealed again.
Note: Please copy the secret key value and store it securely every time you create one. Due to security reasons, we do not store secrets in plain text. As a result, neither you nor anybody else can restore the value once dialog is closed.
Once you have your client id and secret, the second step is to exchange such keys to get an access token. An access token is required to be present during all API calls. Please take a look at the command line example below describing how to get a JWT access token and submit an API call.
curl --request POST \
--url 'https://api.perforator.io/oauth/token' \
--header 'content-type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data client_id=YOUR_CLIENT_ID \
--data client_secret=YOUR_CLIENT_SECRET
{
"access_token":"eyJhbGciOiJ..FTiyY",
"token_type":"bearer",
"expires_in":28800
}
curl --request GET \
--url https://api.perforator.io/v1/projects \
--header 'Accept: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJ..FTiyY'
[
{
"uuid": "8276840e-26ee-41b5-a8ba-ad635759e1dc",
"name": "example.com - Stage",
"notes": "Load Testing in stage environment",
"createdAt": 1624416790568,
"updatedAt": 1624502433458
},
{
"uuid": "4f5fa566-ba8c-4273-ba6b-890f8d6f954f",
"name": "example.com - Prod",
"notes": "Stress Testing of in prod environment",
"createdAt": 1624503190568,
"updatedAt": 1624588833458
}
]
Prebuilt API Client.
Please take a look at our GitLab repository "Perforator SDK Java" if you are interested in what we have released so far.
You can find the source code for the client at the api-okhttp-gson module.
Execute `mvn --version` in your terminal to validate both.
package com.example.projects;
import io.perforator.sdk.api.okhttpgson.ApiClientBuilder;
import io.perforator.sdk.api.okhttpgson.model.Project;
import io.perforator.sdk.api.okhttpgson.operations.ProjectsApi;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ListProjectsExample {
Logger logger = LoggerFactory.getLogger(ListProjectsExample.class);
//Please replace YOUR_CLIENT_ID with you own client id
String apiClientId = "YOUR_CLIENT_ID";
//Please replace YOUR_CLIENT_SECRET with you own client secret
String apiClientSecret = "YOUR_CLIENT_SECRET";
public void run() throws Exception {
ApiClientBuilder builder = new ApiClientBuilder(apiClientId, apiClientSecret);
ProjectsApi projectsApi = builder.getApi(ProjectsApi.class);
List<Project> projects = projectsApi.listProjects();
logger.info("There are {} projects", projects.size());
for (Project project : projects) {
logger.info(
"Project: key={}, name={}, notes={}, createdAt={}, updatedAt={}",
project.getUuid(),
project.getName(),
project.getNotes(),
project.getCreatedAt(),
project.getUpdatedAt()
);
}
}
public static void main(String[] args) throws Exception {
new ListProjectsExample().run();
}
}
OpenAPI Generator.
Open API Specification format allows automatic API client code generation for all major programming languages.
For example, all Perforator-based load generators utilize autogenerated API client.
For sure, you can implement your own API client to our platform via manually writing the code and following this documentation.
Still, we strongly encourage you against doing so because it is a very error-prone and time-consuming process.
It is much easier to utilize tools like
OpenAPI Generator
and automatically generate source code for API client.
npm install @openapitools/openapi-generator-cli -g
openapi-generator-cli generate \
-i https://api.perforator.io/oas.yml \
-g java \
--library okhttp-gson \
--additional-properties groupId=io.perforator.client \
--additional-properties artifactId=perforator-client-api-okhttp-gson \
--additional-properties artifactVersion=1.0.0-SNAPSHOT \
--additional-properties apiPackage=io.perforator.client.api.okhttpgson.operations \
--additional-properties modelPackage=io.perforator.client.api.okhttpgson.model \
--additional-properties invokerPackage=io.perforator.client.api.okhttpgson.invoker \
--additional-properties serializationLibrary=gson \
--additional-properties useGzipFeature=true \
--additional-properties dateLibrary=java8 \
--additional-properties java8=true \
-o perforator-client-api-okhttp-gson