-
Notifications
You must be signed in to change notification settings - Fork 0
Tag Management API: Examples
This section illustrates use of the Tag Management API through the use of comprehensive examples of realistic scenarios.
In this example, we will take a small data set representing an inventory list and create a new tag for each item in it.
Our "inventory.csv" file contains comma-delimited data for "SKU", "Name", and "Description"
item-001,Item One,An Item One in every pot
item-002,Item Two,Item Two for what ails ya
item-003,Item Three,An Item Three in every garage
item-004,Item Four,Item Four will open the door
item-005,Item Five,Item Five is staying alive
item-006,Item Six,Item Six pick up sticks!
This script performs the following:
- Reads each line from our file
- Splits each line's data based on the comma as a separator to get the different fields
- Sends an HTTP POST for each item in our inventory
- Writes out the full response body for each document to a response.$sku.txt for inspection if necessary
The curl command here provides a clean, abbreviated response that shows only the HTTP Status Code and the URL for this new item.
#!/usr/bin/env bash
while IFS=, read sku name description ; do
echo "Uploading: $sku ($name):"
description=${description%?} # Remove newline
curl -w " %{http_code} %{url_effective}\\n" \
-H 'X-ActiveTag-AccessToken: 16cc9f55-8c34-433e-af7a-0054884f7648' \
-X POST https://api.taglabsinc.com/tags/item/$sku \
--data-binary @- \
-o response.$sku.txt 2> /dev/null \
<<EOF
{
"title": "$name",
"description": "$description",
"category": "Web Items",
"thumbnail": "http://www.example.com/images/$sku.png",
"action": {
"type": "url",
"data": "http://www.example.com/$sku.html"
}
}
EOF
done < inventory.csvWhen run, it produces the following result. Note the 201 "Created" response: this is the success message we want.
./upload_inventory.sh
Uploading: item-001 (Item One):
201 https://api.taglabsinc.com/tags/item/item-001
Uploading: item-002 (Item Two):
201 https://api.taglabsinc.com/tags/item/item-002
Uploading: item-003 (Item Three):
201 https://api.taglabsinc.com/tags/item/item-003
Uploading: item-004 (Item Four):
201 https://api.taglabsinc.com/tags/item/item-004
Uploading: item-005 (Item Five):
201 https://api.taglabsinc.com/tags/item/item-005
Uploading: item-006 (Item Six):
201 https://api.taglabsinc.com/tags/item/item-006
We can also verify that these tags have all been created.
curl -X GET \
-H "X-ActiveTag-AccessToken: 16cc9f55-8c34-433e-af7a-0054884f7648" \
"https://api.taglabsinc.com/tags/items"
["item-002","item-004","item-001","item-005","item-006","item-003"]
In this example, we will retrieve a list of our tags and then iterate over that, downloading each QR code with the default PNG settings.
This script executes as follows:
- Retrieve a list of all tags
- Strip off the JSON array formatting and set each item on its own line
- Loop over each item, downloading its QR code as a PNG image
- Verify the success of the download by outputting basic file information
#!/usr/bin/env bash
item_array=$(curl -X GET \
-H 'X-ActiveTag-AccessToken: 16cc9f55-8c34-433e-af7a-0054884f7648' \
https://api.taglabsinc.com/tags/items 2> /dev/null)
for i in $(echo $item_array | sed -e 's/\["//g; s/"\]//g; s/","/\'$'\n/g'); do
echo "Downloading QR code for $i:"
curl -X GET \
-H 'X-ActiveTag-AccessToken: 16cc9f55-8c34-433e-af7a-0054884f7648' \
-o $i.png \
https://api.taglabsinc.com/tags/item/$i/qrcode 2> /dev/null
echo " Got" $(file $i.png)
done
$ ./download_qrcodes.sh
Downloading QR code for item-003:
Got item-003.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
Downloading QR code for item-004:
Got item-004.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
Downloading QR code for item-001:
Got item-001.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
Downloading QR code for item-006:
Got item-006.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
Downloading QR code for item-005:
Got item-005.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
Downloading QR code for item-002:
Got item-002.png: PNG image data, 528 x 528, 1-bit colormap, non-interlaced
In this example, we will create a single web page in PHP that acts as a template capable of handling an unlimited number of tags. It will receive tag data in the URL and will programmatically request this data from the API, showing a single page that changes its contents in response to the URL. Each individual tag is configured to provide a URL that maps back to itself, making it easy to use the API to provide the correct content for the page.
These can be set via the API or by the Administrative Portal.
- Each tag has been configured to point to the web page below
- Each tag's URL also specifies its own "Item ID" required to perform the subsequent API call
- Each tag's description field provides all additional tag-specific communication
The script executes as follows:
- Read the "item-id" from a query parameter in the URL
- Send an API request to get the data for the item
- Paint the web page with the data retrieved from the API response
<?php
$accessToken = '184e5586-83b2-4615-a25d-8b9ee51eedec';
$apiUrl = "https://api.taglabsinc.com/tags/item/{$_GET['id']}";
$apiResult = curlGet($apiUrl, $accessToken);
$apiResult = json_decode($apiResult, TRUE); // Get an associative array
?>
<html>
<head>
<title><?php echo $apiResult['title']; ?></title>
</head>
<body>
<p><img src="<?php echo $apiResult['thumbnail']; ?>" /></p>
<p><?php echo $apiResult['description']; ?></p>
</body>
</html>
<?php
function curlGet($url, $accessToken) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("X-ActiveTag-AccessToken: $accessToken"));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}Copy this script to a directory on your web server. If the URL for this script is http://www.mydomain.com/tag-template.php, then each tag would look like http://www.mydomain.com/tag-template.php?id=item-id-here when configured through the API or the Portal.
When the mobile device scans your tag, this will be the web page that is shown. This web page will take the "id" from the query parameter and use it to query the API server. The API server will find the item with the same "id" and will return the correct data to be safely displayed by this web page. Easy!