Code Samples

Learn how our API works with simple code samples in your preffered language.
If you are looking for quick ways of getting what you need and you don’t have time for writing much code, here we propose you a collection of useful snippets that you can just copy/paste and execute at your convenience.

List your Bare Metal Servers

This snippet will allow you to list all your bare metal servers.

key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X GET -H "X-Lsw-Auth: $key" "https://api.leaseweb.com/v1/bareMetals"
<?php

$resource = '/bareMetals';
$apiKey   = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

$apiUrl   = 'https://api.leaseweb.com/v1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . $resource);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Lsw-Auth: $apiKey"));
$output = curl_exec($ch);

echo $output;

<?php

$method   = "GET";
$resource = "/bareMetals";
$apiKey   = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

$apiUrl    = "https://api.leaseweb.com/v1";
$ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\n"));
$context   = stream_context_create($cparams);
$fp        = fopen($apiUrl . $resource, "rb", false, $context);
$result    = stream_get_contents($fp);

echo $result;


import httplib

# Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals)

connection = httplib.HTTPConnection('api.leaseweb.com', 443, timeout = 30)

# Headers

headers = {"X-Lsw-Auth":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

# Send synchronously

connection.request('GET', '/v1/bareMetals', None, headers)
try:
    response = connection.getresponse()
    content = response.read()
    # Success
    print('Response status ' + str(response.status))
except httplib.HTTPException, e:
    # Exception
    print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/bareMetals"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"GET";
request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Request Operation

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Progress & Completion blocks

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    NSLog(@"Received %lld of %lld bytes", totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: Status Code %ld", operation.response.statusCode);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error.localizedDescription);
}];

// Connection

[operation start];

<div class="panel panel-default">
     <div class="panel-heading"><h3 id="list_domain_names" class="panel-title">List your Domain Names</h3></div>
     <div class="panel-body">
<p>This snippet will allow you to list all your domains.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#b1" data-toggle="tab">CLI</a></li>
<li><a href="#b3" data-toggle="tab">PHP (curl)</a></li>
<li><a href="#b4" data-toggle="tab">PHP (plain)</a></li>
<li><a href="#b5" data-toggle="tab">Python</a></li>
<li><a href="#b6" data-toggle="tab">Objective C</a></li>
</ul>
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X GET -H "X-Lsw-Auth: $key" "https://api.leaseweb.com/v1/domains"
<?php

$resource = '/domains';
$apiKey   = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

$apiUrl   = 'https://api.leaseweb.com/v1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . $resource);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Lsw-Auth: $apiKey"));
$output = curl_exec($ch);

echo $output;
<?php

$method   = "GET";
$resource = "/domains";
$apiKey   = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

$apiUrl    = "https://api.leaseweb.com/v1";
$ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\n"));
$context   = stream_context_create($cparams);
$fp        = fopen($apiUrl . $resource, "rb", false, $context);
$result    = stream_get_contents($fp);

echo $result;
import httplib

# Request: LeaseWeb API (https://api.leaseweb.com/v1/domains)

connection = httplib.HTTPSConnection('api.leaseweb.com', 443, timeout = 30)

# Headers

headers = {"X-Lsw-Auth":"xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx"}

# Send synchronously

connection.request('GET', '/v1/domains', None, headers)
try:
    response = connection.getresponse()
    content = response.read()
    # Success
    print('Response status ' + str(response.status))
except httplib.HTTPException, e:
    # Exception
    print('Exception during request')
// Request: LeaseWeb API (https://api.leaseweb.com/v1/domains)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/domains"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"GET";
request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil];
[connection start];

List your services

This snippet will allow you to list all your LeaseWeb services.
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X GET -H "X-Lsw-Auth: $key" "https://api.leaseweb.com/v1/services"
<?php

$resource = '/services'; $apiKey = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

$apiUrl = 'https://api.leaseweb.com/v1'; $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $apiUrl . $resource); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTHTTPHEADER, array("X-Lsw-Auth: $apiKey")); $output = curlexec($ch);

echo $output;

<?php

$method = "GET"; $resource = "/services"; $apiKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

$apiUrl = "https://api.leaseweb.com/v1"; $ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\n")); $context = streamcontextcreate($cparams); $fp = fopen($apiUrl . $resource, "rb", false, $context); $result = streamgetcontents($fp);

echo $result;

import httplib

Request: LeaseWeb API (https://api.leaseweb.com/v1/services)

connection = httplib.HTTPSConnection('api.leaseweb.com', 443, timeout = 30)

Headers

headers = {"X-Lsw-Auth":"xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx"}

Send synchronously

connection.request('GET', '/v1/services', None, headers) try: response = connection.getresponse() content = response.read() # Success print('Response status ' + str(response.status)) except httplib.HTTPException, e: # Exception print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/services)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/services"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"GET"; request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil]; [connection start];

Reboot your Bare Metal Server

This snippet will allow you to reboot your server, provided you have its ID.
serverId=12345 && \
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X POST -H "X-Lsw-Auth: $key" "https://api.leaseweb.com/v1/bareMetals/$serverId/reboot"
<?php

$serverId = '12345'; $resource = '/bareMetals/%s/reboot'; $apiKey = 'xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx';

$apiUrl = 'https://api.leaseweb.com/v1'; $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $apiUrl . sprintf($resource, $serverId)); curlsetopt($ch, CURLOPTPOST, 1); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTPOSTFIELDS, array()); curlsetopt($ch, CURLOPTHTTPHEADER, array("X-Lsw-Auth: $apiKey")); $output = curlexec($ch);

echo $output;

<?php

$method = "POST"; $serverId = "12345"; $resource = "/bareMetals/%s/reboot"; $apiKey = "xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx";

$apiUrl = "https://api.leaseweb.com/v1"; $ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\n")); $context = streamcontextcreate($cparams); $fp = fopen($apiUrl . sprintf($resource, $serverId), "rb", false, $context); $result = streamgetcontents($fp);

echo $result;

import httplib

Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/reboot)

connection = httplib.HTTPConnection('api.leaseweb.com', 443, timeout = 30)

Headers

headers = {"X-Lsw-Auth":"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"}

Send synchronously

connection.request('POST', '/v1/bareMetals/12345/reboot', None, headers) try: response = connection.getresponse() content = response.read() # Success print('Response status ' + str(response.status)) except httplib.HTTPException, e: # Exception print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/reboot)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/bareMetals/12345/reboot"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"POST"; request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil]; [connection start];

Re-install your Bare Metal Server

This snippet will allow you to reinstall your bare metal server with Centos 6.
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X POST -H "X-Lsw-Auth: $key" -d "osId=868" "https://api.leaseweb.com/v1/bareMetals/12345/install"
<?php

$method = "POST"; $resource = '/bareMetals/%s/rescue'; $serverId = "12345"; $osId = '868'; $apiKey = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

$apiUrl = 'https://api.leaseweb.com/v1'; $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $apiUrl . $resource); curlsetopt($ch, CURLOPTPOST, 1); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTPOSTFIELDS, array("osId" => $osId)); curlsetopt($ch, CURLOPTHTTPHEADER, array("X-Lsw-Auth: $apiKey")); $output = curlexec($ch);

echo $output;

<?php

$method = "POST"; $serverId = "12345"; $osId = "868"; $resource = "/bareMetals/%s/install"; $apiKey = "xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx";

$apiUrl = "https://api.leaseweb.com/v1"; $ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\nContent-type: application/x-www-form-urlencoded\r\n", "content"=> "osId=$osId")); $context = streamcontextcreate($cparams); $fp = fopen($apiUrl . sprintf($resource, $serverId), "rb", false, $context); $result = streamgetcontents($fp);

echo $result;

import httplib, urllib

Request: LeaseWeb API (http://api.leaseweb.com/v1/bareMetals/12345/install)

connection = httplib.HTTPConnection('api.leaseweb.com', 443, timeout = 30)

Headers

headers = {"X-Lsw-Auth":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

Body

bodyparams = {"osId":"868"} body = urllib.urlencode(bodyparams)

Send synchronously

connection.request('POST', '/v1/bareMetals/12345/install', body, headers) try: response = connection.getresponse() content = response.read() # Success print('Response status ' + str(response.status)) except httplib.HTTPException, e: # Exception print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/install)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/bareMetals/12345/install"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"POST"; request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Body

const char bytes[8] = "osId=868"; request.HTTPBody = [NSData dataWithBytes:bytes length:8];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil]; [connection start];

Launch rescue mode on a bareMetal server

This snippet will allow you to boot your bare metal server in rescue mode.
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X POST -H "X-Lsw-Auth: $key" -d "osId=137" "https://api.leaseweb.com/v1/bareMetals/12345/rescue"; // Use 138 for x86 version
<?php

$method = "POST"; $resource = '/bareMetals/%s/rescue'; $serverId = "12345"; $osId = '137'; // Use 138 for x86 version $apiKey = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

$apiUrl = 'https://api.leaseweb.com/v1'; $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $apiUrl . sprintf($resource, $serverId)); curlsetopt($ch, CURLOPTPOST, 1); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTPOSTFIELDS, array("osId" => $osId)); curlsetopt($ch, CURLOPTHTTPHEADER, array("X-Lsw-Auth: $apiKey")); $output = curlexec($ch);

echo $output;

<?php

$method = "POST"; $serverId = "12345"; $osId = '137'; // Use 138 for x86 version $resource = "/bareMetals/%s/rescue"; $apiKey = "xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx";

$apiUrl = "https://api.leaseweb.com/v1"; $ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\nContent-type: application/x-www-form-urlencoded\r\n", "content"=> "osId=$osId")); $context = streamcontextcreate($cparams); $fp = fopen($apiUrl . sprintf($resource, $serverId), "rb", false, $context); $result = streamgetcontents($fp);

echo $result;

import httplib, urllib

Request: LeaseWeb API (http://api.leaseweb.com/v1/bareMetals/12345/rescue)

connection = httplib.HTTPConnection('api.leaseweb.com', 443, timeout = 30)

Headers

headers = {"X-Lsw-Auth":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

Body

bodyparams = {"osId":"137"} # Use 138 for x86 version body = urllib.urlencode(bodyparams)

Send synchronously

connection.request('POST', '/v1/bareMetals/12345/rescue', body, headers) try: response = connection.getresponse() content = response.read() # Success print('Response status ' + str(response.status)) except httplib.HTTPException, e: # Exception print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/rescue)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/bareMetals/12345/rescue"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"POST"; request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Body

const char bytes[8] = "osId=137"; // Use 138 for x86 version request.HTTPBody = [NSData dataWithBytes:bytes length:8];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil]; [connection start];

Get network usage of a Bare Metal Server

This snippet will allow you to get the network usage of a given bare metal server.
serverId=12345 && \
key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx && \
curl -X GET -H "X-Lsw-Auth: $key" "https://api.leaseweb.com/v1/bareMetals/$serverId/networkUsage"
<?php

$serverId = '12345'; $resource = '/bareMetals/%s/networkUsage'; $apiKey = 'xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx';

$apiUrl = 'https://api.leaseweb.com/v1'; $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $apiUrl . sprintf($resource, $serverId)); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTHTTPHEADER, array("X-Lsw-Auth: $apiKey")); $output = curlexec($ch);

echo $output;

<?php

$method = "GET"; $serverId = "12345"; $resource = "/bareMetals/%s/networkUsage"; $apiKey = "xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxx";

$apiUrl = "https://api.leaseweb.com/v1"; $ctxParams = array("http" => array("method" => $method, "header" => "X-Lsw-Auth: $apiKey\r\n")); $context = streamcontextcreate($cparams); $fp = fopen($apiUrl . sprintf($resource, $serverId), "rb", false, $context); $result = streamgetcontents($fp);

echo $result;

import httplib

Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/networkUsage)

connection = httplib.HTTPConnection('api.leaseweb.com', 443, timeout = 30)

Headers

headers = {"X-Lsw-Auth":"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"}

Send synchronously

connection.request('GET', '/v1/bareMetals/12345/networkUsage', None, headers) try: response = connection.getresponse() content = response.read() # Success print('Response status ' + str(response.status)) except httplib.HTTPException, e: # Exception print('Exception during request')

// Request: LeaseWeb API (https://api.leaseweb.com/v1/bareMetals/12345/networkUsage)

NSURL* URL = [NSURL URLWithString:@"https://api.leaseweb.com/v1/bareMetals/12345/networkUsage"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = @"GET"; request.timeoutInterval = 30;

// Headers

[request addValue:@"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" forHTTPHeaderField:@"X-Lsw-Auth"];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil]; [connection start];