Add beacon attachments using curl

DEFINE ("API_URL", "https://cloud.estimote.com");
DEFINE ("API_URL_DEVICES", "https://cloud.estimote.com/v3/attachments");
DEFINE ("API_ID", "beaczone-kes");
DEFINE ("API_TOKEN", "dd6ee95dab6e039cf6bf194ac69fef87");

$beacon = curl_init ();

$headers = array();
$headers[] = "Accept: application/json";

curl_setopt ($beacon, CURLOPT_URL, API_URL_DEVICES);
curl_setopt ($beacon, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($beacon, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ($beacon, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($beacon, CURLOPT_USERPWD, API_ID . ":" . API_TOKEN);

$postdata = array('data'=>array('payload'=>array('enter'=>'5260-3390-enter','exit'=>'5260-3390-exit'),
                  'identifier'=>'5ab2d6a30e9850230292f7cb1b8e5c16'));

curl_setopt($beacon, CURLOPT_POST, count($postdata));

$postdata = http_build_query($postdata);

//$postdata = json_encode($postdata);

curl_setopt($beacon, CURLOPT_POSTFIELDS, $postdata);

$result = curl_exec ($beacon);

if (curl_errno ($beacon)) {
    echo "\033[31m ERROR \033[0m \n:" . curl_error ($beacon);
} else {
    header ('Content-Type: application/json');
    echo ($result);
}
curl_close ($beacon);die();

When I tried to add beacon attachements in estimote cloud using curl , I am getting error like below.Can anyone help me to solve this asap.

Error:

code 400
message "Unique constraint violation"
name "EstimoteError"

Thanks.
Deep

Hi Deep! The reason you are getting this error is because an attachment for this beacon already exists. Take a look at https://cloud.estimote.com/docs/#api-Attachments-CreateAttachment you are getting an error like 400_Bad_Request Attachment for identifier already exists

It seems that you are trying to do is to update the payload https://cloud.estimote.com/docs/#api-Attachments-UpdateAttachmentPayload

@JGrzesik Thanks For your help.

I have just try to create attachements for my another fresh beacon & its working fine. But below is the scenario in which estimote curl api to create attachments not working.

Step 1 : Create attachemets using curl api for fresh or new beacon.(working fine)

Step 2: Now remove attachments from estimote cloud login under edit settings of that beacon. (removed successfully)

Step 3: Again try to Create attachemets using curl api for the same beacon(means step 1 repeat) – here i got error everytime.

Error:

code 400
message "Unique constraint violation"
name "EstimoteError"

So conclusion is that for the first time i can create attachments for particular beacon but if i remove that attachments from estimote cloud login for that beacon , I am not able to create attachments again for that beacon.

Can you please follow the above steps to raise an issue so that we can sort out asap.

Thanks,
Deep

Hi Deep! Thank you for pointing this out! In order to completely remove attachment you have to use this method: https://cloud.estimote.com/docs/#api-Attachments-DeleteAttachment

If you remove all keys and values via Cloud UI, the attachment will be empty (an empty object in payload field) but it will not be deleted.

Hope that helps!

@JGrzesik Ok Got it.

But here I am confused that if by mistakenly I have removed attachments from Cloud UI, that means after that I will not able to create attachments in future for that beacon using curl api.

so is that true way to achieve functionality technically.

One more thing if i wanted to delete attachments using curl api than where can I get ATTACHMENT_ID to delete it.

Thanks,
Deep.

Hi Deep!

To find id of an attachment use this method:
https://cloud.estimote.com/docs/#api-Attachments-GetAttachments

In your case the request should look like this:
https://cloud.estimote.com/v3/attachments?identifiers=YOUR_DEVICE_IDENTIFIER
Then in response you should find your Attachment, it’s id is the ATTACHMENT_ID you need.

@JGrzesik Ok thanks , I got ATTACHMENT_ID as per your suggestion.

But I am still confusing what happens if by someone or by me mistakenly remove attachments from cloud UI than which steps I have to perform to make it working again to add attachments using curl api.

Because as per your understanding if I removed attachments by mistakenly from estimote cloud UI than i have to compulsory make call of delete or update api to create attachments again.

Please suggest best way to create attachments if i removed it from estimote cloud UI also with miniumum line of code.

Hope you got my point.

Hi Deep!

If you are trying to assign the same key-value pairs to multiple beacons you select multiple beacons in Estimote Cloud, press “Edit” and use attachment tab to enter keys and values and then press “Save Changes”. This way you would update existing attachments and create new ones if some are missing. Using this method you can create hundreds of attachments with just a few clicks.

Another way, if you want to use CURL would be to use this endpoint https://cloud.estimote.com/docs/#api-Attachments-GetAttachments without any query parameters it should return a list of all attachments in your account, to detect any attachments that are empty ("payload": {}).
Manually delete them using the delete endpoint. After that cleanup your script should work.

Take a look at this example tool https://github.com/Estimote/Android-Proximity-SDK/tree/master/tools/migration_from_ibeacon_and_tags_to_attachments
perhaps it be an inspiration for your scripts.