In 10 lines (ok, it’s actually 11 excluding whitespace) of code; A simple Perl LWP function that can perform any Dreamost API call and return the result as a Perl data structure. After writing this function I was able to hack out a simple Dynamic DNS updater script in under 30 minutes.
my $agent = LWP::UserAgent->new;
sub api_call {
my $cmd = shift;
my $post_data = shift;
$post_data->{key} = "6SHU5P2HLDAYECUM";
$post_data->{cmd} = $cmd;
$post_data->{format} = 'perl';
my $resp = $agent->post("https://api.dreamhost.com/", $post_data);
return new Safe()->reval($resp->decoded_content) if ($resp->is_success);
croak "API call $cmd failed: " . $resp->status_line;
}
Edit: I had the function down to 10 lines, but I accidentally introduced a bug and the resulting code was somewhat less readable. I’ll take another crack at slimming it down later.
While the subroutine has been slightly compressed to hit the 10 line goal, it’s still perfectly readable. One caveat to using the above code is that you need to have Crypt::SSLeay installed as the Dreamhost API operates over HTTPS.
# list all dns records
my $records = api_call("dns-list_records");
print Dumper($records);
# create an alias dns record for a subdomain
my $result = api_call("dns-add_record", {
record => "subdomain.pointyspoon.com",
type => "A",
value => "192.168.0.1",
comment => "DNS forwarding alias"
});
croak "dns-add_record failed: " . $result->{data} if ($result->{result} ne "success");
Of course you could always use the WWW::DreamHost::API module, but ironically DreamHost doesn’t include this module on their own servers.
Happy Dreamhost API Hacking!

Jim
/ September 13, 2010You posted your API key in your example.
Brian Cowdery
/ September 13, 2010Thanks Jim!
I thought for sure I had replaced it with the testing API key from the Dreamhost Wiki… I guess I needed to look more closely before posting. The old key has been invalidated and I’ve updated the article.