Basecamp API example: listing your projects
April 13, 2006
I’ve been using Basecamp recently to manage my clients (more on that in another post) and was recently intruigued when the API became available. Put simply, an API enables you to access your data not only through logging into the site but also by writing programs to access/manage your data.
I’m sure people will come up with some amazing uses of the Basecamp API (e.g. Project Detail), but for now there are few (even simple) examples. I played around with some code today because I’ve had a simple use in mind.
I’ve just been too busy lately to update my main site - as much as I want to - but I’ve realised that I can update a part of my main site thanks to the Basecamp API. I’ve written a function to enable me to show current, recent and past projects I’m doing and soon I’ll put that on the home page. You can see the unformatted version here. If you’d like to use/adapt the code, read on.
Please note: this code can probably be vastly improved on, but for now it works. Any suggestions, please let me know.
Here’s the code I’m using now:
<?php
// List projects
$request = 'http://mysite.clientsection.com/project/list';
$session = curl_init();
$user = "username";
$password = "password";
curl_setopt($session, CURLOPT_URL, $request);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt ($session, CURLOPT_POST, 1);
curl_setopt ($session, CURLOPT_POSTFIELDS, $xml);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$user . ":" . $password);
if(ereg("^(https)",$request)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$request = curl_exec($session);
curl_close($session);
include('xml.php');
$arr = XML_unserialize($request);
$arr = $arr["projects"]["project"];
function showprojects($projects,$type,$hide=array(),$lastupdated=true){
$hide = array($hide);
echo “<h2>”;
if ($type == “active”)
echo “Current”;
elseif ($type == “on_hold”)
echo “Recent”;
elseif ($type == “archived”)
echo “Past”;
echo ” projects</h2>\n<ul>\n”;
foreach($projects as $key => $value){
if ($value["status"] == $type && !(in_array($value["name"],$hide))){
echo “<li>”;
if ($value["announcement"] != “”)
echo “<a href=\”" . $value["announcement"] . “\”>”;
echo $value["name"];
if ($value["announcement"] != “”)
echo “</a>”;
if ($lastupdated)
echo ” (” . substr($value["last-changed-on"],0,10) . “)”;
echo “</li>\n”;
}
}
echo “</ul>\n”;
}
showprojects($arr,”active”,”Life”);
showprojects($arr,”on_hold”);
showprojects($arr,”archived”);
?>
My function enables you to hide projects you don’t want the world to know about (e.g. “Life”).
I have used Keith Devens PHP XML Parser, so you will need to download that file and put it in the same directory as my script.






Nice piece of code, I would bet that there’ll be alot more snippets coming out once people have had more time to play around with the API. One thing that would be really interesting would be to get some interaction going between Google Calendar and Basecamp, but unless Google release an API for the calendar that’s not going to be possible.
— garrett, April 13, 2006
Hi Rachel,
Have you seen the work these guys are doing?
http://www.projectdetail.com/
— Tim Beadle, April 14, 2006
Hi Tim, yes I link to them in my post
— Rachel, April 14, 2006
Hi Rachel & Tim,
Thanks for noticing my work!
Rachel — I like how you are pulling the URL from the “announcement”.
Although the things I’m working on creating have additional data stored in other databases, I think it is important when developing add-on interfaces to the API to creatively store and retreive as much meta-data as possible in Basecamp itself so that you can see it in that interface as well — like you are doing here with the URL.
Project Detail’s Support Tickets store the ticket number in [square brackets] in the Basecamp Message title, and the To Do Minder will have an option to append the reminder date/time to the end of the To Do item when you set a reminder.
Keep on coding!
— Chris Busse, April 15, 2006
Rachel,
Oops
That’ll teach me to be more careful when I speed-read 
— Tim Beadle, April 20, 2006