Update 2019: UCSC does have an API now: http://api.genome.ucsc.edu
What I wrote below nine years ago when I was a postdoc, not at UCSC and on a different continent, may still be relevant, as the API doesn't cover everything yet.
Also UCSC has a page about the bin field now: http://genomewiki.ucsc.edu/index.php/Bin_indexing_system
External (Ruby or Python) APIs
You can try to play around with a hacked-together Ruby or Python API that accesses the tables via MySQL. You could write your own in Java. But they are not supported officially and just hacked together by a single programmer. In my opinion, they may prove to waste your time in the end.
Actually, you do not need an API, the UCSC table browser provides ample help to construct SQL commands, describing each and every field and all relationships between all tables. You can then access the tables yourself with your MySQL client (though the binning scheme will make them a lot faster, as Heng wisely pointed out), no need for an API. I see this as an advantage, as you have one dependency less, no external library that can break and messy updates.
* Avoid DAS *
Direct MySql access is more general than using the overly complex DAS XML format, which will only give you the chrom-start-stop-like annotations, not X-Y plots and not any of the special formats that UCSC is using (chain, net, psl, wiggle, maf, etc). There is no point in using the UCSC database if you cannot access the advanced data fields.
A book?
Peter Schattner's book is interesting in that he writes the same software with several different APIs and you can compare the implementations. But I am unsure if I would buy it just for the two chapters (chapter 9 and 10, 148 -214). They offer, however, a very good introduction to the topic, so you might buy it nevertheless.
The UCSC API
The true power of the UCSC API and the key to its speed is only accessible from C, because the genome browser is written in C and so is the API. If you know some C you should be able to figure out how it works quite quickly. Download the source of the UCSC tools and compile the libraries and tools, following the instructions on http://genome.ucsc.edu/admin/jk-install.html.
Then, this is the most important part, search for something that is similar to what you are planning to do. Want to parse a 2bit file - look at twoBitToFasta.c. Want to get information on how to load tables into C structures - look at e.g. featureBits.c. Want to know how to map between genomes - look at liftOver.c. And so on. Copy-and-pasting will get you very far, given that there are >150k lines of code to look at. And, to take into account Heng's comment, it will take care of the bin-field automatically.
Don't forget that everything that the UCSC guys do is very well documented in their makeDB files on http://hgwdev.cse.ucsc.edu/~kent/src/unzipped/hg/makeDb/doc/ and that a lot of stuff is documented at other places (use my page on http://genomewiki.ucsc.edu/index.php/Learn_about_the_Browser as a reference sheet). The makeDb files should show you which tool you need to look at. If you don't know what tool is most similar to your task, then send an email to their mailing list to ask for the name of a tool that does xyz - there are >750 tools in the source tree, so there often is something already somewhere in their code.
When you're doing this the first time, it will take more time to set up than if you use a famous Perl Bioinformatics API, but it will produce stable and very very fast code. You can also any genomics problem with the API and will be able to use the code over years. The example in Schattner's book is several times (219 seconds with the Ensembl API versus 6 seconds with the UCSC API, page 176, second paragraph) faster than the version with the Perl API. In addition, your C code will never break due to a version change somewhere on the internet, as it run on local textfiles.
Given what you want to get, I'd suggest the ruby-ensembl-api :-)
I've added an example of how to print exons and introns using the ensembl API in examples_perl_tutorial.rb
To make the library efficient, one should change
overlap_sql()
in https://github.com/jandot/ruby-ucsc-api/blob/master/lib/ucsc/hg18/activerecord.rbNo, that API will be very inefficient. All the magic of UCSC is the "bin" field. If you do not use that, you will lose most of the power of UCSC!
Which tracks do you need access to?
Here is a description of the bin field, with links to implementations in C, Perl, Python and Ruby: http://genomewiki.ucsc.edu/index.php/Bin_indexing_system