Getting started

This isn’t rocket science

Ready to unlock the power of the Unity API? Let’s get you started. Don’t worry if this is the first API you’re working with, you’ll soon notice this isn’t rocket science. We’ll cover everything from what tools to use to your very first “Hello World” API interaction. Put your propeller-hat on, this is where the fun starts.

API interaction

Any tool for making HTTP requests is suitable to work with the Unity API. Unity API is a RESTful API and follows most of the common agreements on HTTP Methods usage. Communication is done in JSON format so any request to the API should be a valid JSON document.

Requests

All request to the API should be sent via HTTP protocol using the right HTTP method for the call (see the table below for when to use which method). Requests that contain a request body should have a Content-Type HTTP header with a description of content being sent (typically application/json). Also, every request should be signed with a personal access token placed in the header. Sign-up to get your access token (it’s free).

Request example

Authorization: Bearer MWQzMjQ2NjU1NjgxMGQwMzI3MzY0OTE5NmU0ZTQ2NzE3YzYxNmVkZGYxNGE1MWVjOGVkMjZlNjVjMzQ1NDY1NQ

HTTP methods

MethodUsage
GETUsed for information retrieval. Any request using the GET method is read-only and will not affect any of the objects you are querying.
POSTUsed for object creation. The request should contain all the required fields for an object that is being created. It can also be used for performing an action, such as a “force irrigation” action.
PUTUsed for updating an object.
PATCHUsed for bulk operations on multiple objects.
DELETEUsed to remove any object or collection of objects.

 

HTTP statuses

Each API response contains a corresponding HTTP Status Code. Codes in the 200 to 299 range indicate a successful action and means that no error was encountered. Codes in the 400 to 499 range indicate that there was an issue with the request that was sent. You should check your code before trying again. Codes in the 500 to 599 range indicate a server side problem. It means that the Unity platform is having an issue and cannot fulfill your request at this time. Try again later.

HTTP responses

Typically every response contains a response body as a JSON document. An exception to this are DELETE requests. They always return an HTTP status of 204 No Content along with an empty response body. Most of the responses contain a requested object or a list of objects. In some cases results are wrapped in a structure that includes pagination info.

Response example with pagination

{
	"controllerList": [
		{
			"id": "example string value",
			"serialNumber": "example string value",
			"activated": true,
			"projectId": "example string value"
		}
	],
	"page": 843,
	"size": 213,
	"total": 525
}

HTTP clients

As said before, you can use any HTTP client to interface with the Unity API. To get your started, below is an overview of the most commonly used ones.

cURL

cURL is installed by default in most popular *nix distributions. It is a widely spread tool and a library for making HTTP requests. cURL is a command line utility and does not have any GUI.

Simple request example

curl 'https://developer-api.etwater.com/api/v1/hello-unity'

Response

{
	"greetings": "Hello from Unity platform",
	"requestMethod": "GET"
}

The request method should be set using the -X console parameter:

PUT request example

curl -X PUT 'https://developer-api.etwater.com/api/v1/hello-unity'

Response

{
	"greetings": "Hello from Unity platform",
	"requestMethod": "PUT"
}

Request headers should be set using the -H console parameter:

Content-Type and Authorization headers example

curl -X PUT -H'Content-Type: application/json' -H'Authorization:  Bearer $ACCESS_TOKEN' 'https://developer-api.etwater.com/api/v1/hello-unity/auth'

Note that in the above request $ACCESS_TOKEN is a placeholder and should be replaced with your real access token you received when signing-up for API access.

Response

{
	"greetings": "Hello from Unity platform",
	"requestMethod": "PUT",
	"isAuthenticated": true
}

POSTMAN

On of the most popular GUI applications to interact with HTTP web services is POSTMAN. It has an intuitive UI and is available on all major platforms as a native application or as a Google Chrome extension.

POSTMAN request and response examples

In POSTMAN that last cURL request will look like this:

And as an added benefit you can generate cURL requests from within the POSTMAN UI:

PHPStorm IDE REST Client

PHP, as the most commonly used language in web development, is often used with a PHPStorm IDE. This IDE has a built-in REST Client UI tool that could be used to interact with the Unity API.

You can find the REST client under the Tools menu section:

And that last cURL example in this client will look like:

In general, everything remains the same, except you have an easier way to set HTTP request options with the IDE UI.

Next steps

Now that you know how to interact with the Unity API and have several tools at your disposal to get started, it’s time to get to know the main API endpoints, and after that make sure you check out the full API specification.