Particeep

Getting started

Learn how to make your first request on the Particeep API
During this tutorial, we are going to use a test environment with a common API Key and secrets.

Please follow the instructions here to get your test, production key and secret.
To switch in between environments you just need to change the base URL, API key and secret. Everything else will work the exact same way.
NB: We will use the Particeep’s core API for this tutorial. Our insurance for API follows the same mechanisms for authorization;you just need to change the base URL and API key/secret. Click here to see the details.

Public access
base url
https://test-api.particeep.com
key
d6a53e1a-fc8e-4251-9dda-fabbce5f2a2c
secret
9bb3c122-0272-4bed-a632-19d5d52c7b5e

Step 1 - First request

We are going to take care of your API first request. The sample code will be on Java, we will make available a sample in all main languages soon.

The first request is a health check of the api: it always return Ok and doesn’t need any authentication.

NB: We will use the WS lib from the play framework to do the http request. We will use Play a lot of Particeep but you can easily achieve the same with another tool like Apache HttpClient or plain URL.openConnection from the JDK.


				String url = "https://test-api.particeep.com/ping";
		
		HttpResponse result = WS.url(url).get();
		String result = result.getString();
		System.out.println(result);
		

				var request = require("request");

		request("https://test-api.particeep.com/ping", function(error, response, body) {
		  console.log(body);
		});
		

				require 'net/http'

		url = URI.parse('https://test-api.particeep.com/ping')
		req = Net::HTTP::Get.new(url.to_s)
		res = Net::HTTP.start(url.host, url.port) {|http|
		  http.request(req)
		}
		puts res.body
		

				import requests
		r = requests.get("https://test-api.particeep.com/ping")
		print r.content
		

				$response = http_get("https://test-api.particeep.com/ping");
		echo $response
		

				resp, err := http.Get("https://test-api.particeep.com/ping")
		

This request is helpful to verify that our server is running fine.
Then you should have a lip set up to do the remote request and then you are ready to go to the next step: Authentication.

Step 2 - Handling security

Try to do a request on https://test-api.particeep.com/v1/info.
This is an endpoint, which returns to basic data on the API like on the current version. If You use the code from step 1, you should get 403 for the http code which means forbidden and the following body.


		{
	"error" : {
		"hasError" : true,
		"errors" : [
			{
				"message" : "error.api.invalid.key"
			}
		]
	}
}
	

error.api.invalid.key indicate that your credentials are not sent correctly : we will fix that

To get authentified we need to add 2 headers to our request : DateTime and Authorization

DateTime’s header

DateTime is easy : it’s juste the current date in the iso 8601 format. E.g: 2015-10-19T11:04:18Z
With the help of the jodatime library, we can build it that way


			import org.joda.time.DateTime;
	import org.joda.time.DateTimeZone;
	import org.joda.time.format.DateTimeFormatter;
	import org.joda.time.format.ISODateTimeFormat;
	
	private static String buildDateHeader() {
		DateTime date = DateTime.now(DateTimeZone.UTC);
		DateTimeFormatter format = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
		return format.print(date);
	}
	

Autorization’s header

Now let’s build the autorization’s header
Basically the idea is to

Here is the code with comments for each step :


		import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

// you can find it in commons-codec-1.10.jar
import org.apache.commons.codec.binary.Base64;

public static String buildAuthorizationHeader(String apiKey, String apiSecret, String dateTime) throws UnsupportedEncodingException,
			NoSuchAlgorithmException, InvalidKeyException {

	//Concat your keys and DateTime in the following order : APISecret + APIKey + DateTime
	String toSign = apiSecret + apiKey + dateTime;
	
	//Get a list of bytes from the resulting string in UTF-8 format
	byte[] messageBytes = toSign.getBytes("UTF-8");
	
	//Get a list of bytes from your APISecret in UTF-8 format
	byte[] secretBytes = apiSecret.getBytes("UTF-8");
	
	//Sign 'messageBytes' with 'secretBytes' using SHA1.
	Mac mac = Mac.getInstance("HmacSHA1");
	SecretKeySpec signingKey = new SecretKeySpec(secretBytes, "HmacSHA1");
	mac.init(signingKey);
	byte[] result = mac.doFinal(messageBytes);
	
	//Encode the resulting bytes in hexadecimal format.
	char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
	
	int len = result.length;
	char[] hexChars = new char[len * 2];
	
	for (int charIndex = 0, startIndex = 0; charIndex < hexChars.length;) {
	  int bite = result[startIndex++] & 0xff;
	  hexChars[charIndex++] = HEX_CHARS[bite >> 4];
	  hexChars[charIndex++] = HEX_CHARS[bite & 0xf];
	}
	
	// We use lowercase
	String sign = new String(hexChars).toLowerCase();
	
	//Encode the bytes of the result in base 64.
	String signature = new String(Base64.encodeBase64(sign.getBytes("UTF-8")));
	
	//Concat all results like this: PTP:APIKey:signature
	String authorization = "PTP:" + apiKey + ":" + signature;
	
	return authorization;
}
	

Final request

Now we are putting everything together to get a working request


			public static void test() {
		String url = "http://local.particeep.com:9100/v1/info";
		String apiKey = "d6a53e1a-fc8e-4251-9dda-fabbce5f2a2c";
		String apiSecret = "9bb3c122-0272-4bed-a632-19d5d52c7b5e";

		String dateTime = buildDateHeader();
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("DateTime", dateTime);
		headers.put("Authorization", buildAuthorizationHeaderSafe(apiKey, apiSecret, dateTime));

		HttpResponse rez = WS.url(url).headers(headers).get();
		System.out.println("status: " + rez.getStatus());
		System.out.println("json:" + rez.getString());
	}
	
	private static String buildAuthorizationHeaderSafe(String apiKey, String apiSecret, String dateTime) {
		try {
			return buildAuthorizationHeader(apiKey, apiSecret, dateTime);
		} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
			Logger.error(e, "Error while creating autorization header for Particeep's api");
			return "";
		}
	}
	

You should see this response on your console


			status: 200
json: {"version":"1","debugEnable":true,"metaEnable":true}

Next steps

Now you know how to make a request with credentials to Particeep’s API. You may want to