Search

API examples

API examples

These examples use curl to perform some basic API tasks with Varnish Controller. All output and input is JSON-structured data. Full Swagger API documentation can be viewed by starting the api-gw and visiting http://localhost:8002/docs/index.html. The host and port (localhost:8002) are where the api-gw is listening.

Login as sys admin using basic auth

curl -X POST -utest:test http://localhost:8002/api/v1/auth/login

Login as basic auth user of an existing organization

curl** -X POST -utest:test http://localhost:8002/api/v1/auth/login -d '{"org": "myorg"}'

Get all agents

curl -H "Authorization: Bearer <access_token>" http://localhost:8002/api/v1/agents

Get all agents using filtering

curl -H "Authorization: Bearer <access_token>" "http://localhost:8002/api/v1/agents?name=*server*"

Assign tags to agent

curl -H "Authorization: Bearer <access_token>" -X PUT http://localhost:8002/api/v1/agents/1 -d '{"Tags": [{"ID": 2}]}'

Remove all tags from agent

curl -H "Authorization: Bearer <access_token>" -X PUT http://localhost:8002/api/v1/agents/1 -d '{"Tags": []}'

Get VCLs

curl -H "Authorization: Bearer <access_token>" http://localhost:8002/api/v1/files

Get specific VCL

curl -H "Authorization: Bearer <access_token>" http://localhost:8002/api/v1/files/1

Delete existing VCL

curl -H "Authorization: Bearer <access_token>" -X DELETE http://localhost:8002/api/v1/files/1

Deploy VCLGroup

curl -H "Authorization: Bearer <access_token>" -X PUT http://localhost:8002/api/v1/vclgroups/1/deploy

Get API log entries

curl -H "Authorization: Bearer <access_token>" http://localhost:8002/api/v1/apilogs

Get new access/refresh token pair based on refresh token

curl -X POST -H "Authorization: Refresh <refresh_token>" localhost:8002/api/v1/auth/refresh

Errors

If an error occurs for a request, an error structure will be reported back. It will contain a trace ID (used in the system logs from the processes) that can be used to trace the error from the API-GW back to the agents. A human-readable error message and an errorKey that describes the scope of the error is reported back.

Example:

$ curl -H "Authorization: Bearer <access_token>" -X PUT http://localhost:8002/api/v1/vclgroups/2/deploy

{"traceId":"1coNQFLrcGkVcSXrOtfs0VtG3Ua","errorKey":"deploy","errorMsg":"id 2 not found"}%

Filters

For API requests we allow filters and operators on those filters. To see the complete list of available filters, check the API-GW Swagger documentation. The API has support for starts with, ends with or contains filters, as well as any or all filters. The API also supports filtering on relationships.

Operators can be used to change the behavior of filters, for example it is useful to filter everything after a specific date or return all agents that have all or any of the following tags

Equal or not equal

The equal filter filters an exact match.

GET /api/v1/agents?name=agent // Returns all records where name matches 'agent'
GET /api/v1/agents?name[neq]=agent // Returns all records where name matches 'agent'
GET /api/v1/agents?name[neq]=*agent* // Returns all records where name does not contain 'agent'
GET /api/v1/agents?name[neq]=*agent*,*varnish // Returns all records where name does not contain 'agent' and do not end with 'varnish'
GET /api/v1/agents?tags.id[neq]=1,2 // Returns all records that do not have a connected tag with id 1 OR 2

Starts or ends with and contains

GET /api/v1/agents?name=agent* // Returns all records where name starts with 'agent'
GET /api/v1/agents?name=*agent // Returns all records where name ends with 'agent'
GET /api/v1/agents?name=*agent* // Returns all records where name contains 'agent'
GET /api/v1/agents?name=*agent*,*varnish // Returns all records where name contains 'agent' and that do not end with 'varnish'

Any or all of the following

GET /api/v1/agents?tags.id=1,2 // Returns all records that have a connected tag with id 1 OR 2
GET /api/v1/agents?tags.id[all]=1,2 // Returns all records that have a connected tag with id 1 AND 2

Greater than / Greater than equal

GET /api/v1/agents?created[gt]=2020-11-01 // Returns all records where created at is greater than
GET /api/v1/agents?created[gte]=2020-11-01 // Returns all records where created at is greater than or equal to

Lower than / Lower than equal

GET /api/v1/agents?created[lt]=2020-11-01 // Returns all records where created at is lower than
GET /api/v1/agents?created[lte]=2020-11-01 // Returns all records where created at is lower than or equal to

®Varnish Software, Wallingatan 12, 111 60 Stockholm, Organization nr. 556805-6203