RackSpace Cloud API - getting the token and the endpoint
RackSpace is an American hosting provider with several data centers, offering a big number of hosting solutions. One of the products is cloud hosting with its own set of additional services like backups, autoscaling, CDN and so on.
All of it is managed through the API, where authorization is based on a username and a token. The token is valid for one day only, so every script that works with the API has to start with getting a valid token.
The generic request looks like this:
curl -s -X POST https://identity.api.rackspacecloud.com/v2.0/tokens \
-H "Content-Type: application/json" \
-d '{
"auth": {
"RAX-KSKEY:apiKeyCredentials": {
"username": "<strong>login</strong>",
"apiKey": "<strong>api-key</strong>"
}
}
}' | python -m json.tool
login is your login in RackSpace cloud
api-key can be found on the account settings page, as shown on the screenshot:

The response comes in json. Unfortunately parsing json with bash is very inconvenient, unlike php.
The following function takes the username and the api-key as arguments and returns the token:
<?php
function get_cloud_token($username,$apiKey) {
$url="https://identity.api.rackspacecloud.com/v2.0/tokens";
$post_string = '{"auth": { "RAX-KSKEY:apiKeyCredentials": { "username": "'.$username.'", "apiKey": "'.$apiKey.'" } } }';
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
$result = curl_exec($curl_connection);
$data = json_decode($result);
$token = $data->access->token->id;
return $token;
}
?>
The json you get back from the first request also contains the endpoints for the different services:
- cloudFilesCDN
- cloudFiles
- cloudBlockStorage
- cloudImages
- cloudQueues
- cloudBigData
- cloudOrchestration
- cloudServersOpenStack
- autoscale
- cloudDatabases
- cloudBackup
- cloudNetworks
- cloudMetrics
- cloudLoadBalancers
- cloudFeeds
- cloudSites
- cloudMonitoring
- cloudDNS
- cloudServers
- rackCDN
The endpoint is the address of the server, depending on the data center and the service you need to talk to.
The following function takes the username, the api-key, the data center and the service name as arguments. It returns the endpoint for the other requests:
<?php
function get_endpoint($username,$apiKey,$datacenter,$service) {
$url="https://identity.api.rackspacecloud.com/v2.0/tokens";
$post_string = '{"auth": { "RAX-KSKEY:apiKeyCredentials": { "username": "'.$username.'", "apiKey": "'.$apiKey.'" } } }';
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
$result = curl_exec($curl_connection);
$data = json_decode($result);
foreach ($data->access->serviceCatalog as $service) {
if ($service->name == $service) {
foreach ($service->endpoints as $endpoint) {
if ($endpoint->region == $datacenter) {
$endpointURL = $endpoint->publicURL;
}
}
}
}
return $endpointURL;
}
?>
There are 6 data centers in total:
- ORD - Chicago
- DFW - Dallas/Ft. Worth
- HKG - Hong Kong
- LON - London
- IAD - Northern Virginia
- SYD - Sydney
Original documentation: