Using the Typekit API from Javascript

As part of our ongoing work on the Typekit API, we’ve added support for JSON with callbacks. This means you can now easily use Typekit data in your Javascript code.

JSON with callbacks

JSON with callbacks, also known as JSONP, is an alternative to XmlHTTPRequest that avoids the same origin policy. In other words, it allows code on your site to request data from our site.

Most libraries have built in support for JSONP. For example, we can fetch information about Typekit’s library using jQuery:

var url = "";
$.getJSON(url, function(data){
  var count = data.library.pagination.count;
  alert('There are ' + count + ' fonts in the full library');

The URL we’re requesting includes a callback parameter that tells the Typekit API you want JSONP output. jQuery does the hard work of requesting the data and then turning it into a Javascript data structure for you to read from.

A step-by-step example

So what can we use this for? One small but useful thing we’ve built is a bookmarklet that tells you which Typekit fonts are used on a page.

We start out by iterating over the page looking for the Typekit embed code:

var kitId;
  var m = this.src.match(/use\.typekit\.com\/(.+)\.js/);
  if (m) {
    kitId = m[1];

One we have the kit ID we make an API call for more information about that kit:

$.getJSON("" + kitId 
          + "/published?callback=?", function(data){
  // …

Then we use that data to generate the HTML for a box containing a list of font families used in the kit:

var box = $("

Typekit fonts used on this page:

") $.each(data.kit.families, function(i,family){ var item = $('

' + + '

'); item.appendTo(box); });

We then give that box some style, and insert it into the page body:

      'border':"1px solid #ddd",
      'color': '#666',
      'position': 'absolute',
      'right': '5px',
      'width': '200px'

And we’re done!

Learning more

You can view the full code for the bookmarklet and instructions on how to use it over at github. You can also check out the full documentation on the output formats supported by the Typekit API.

If you have questions or feedback, let us know in the comments, or jump into the typekit-dev google group. We look forward to hearing from you.

2 Responses

  1. Hello, Congratulations on ALL you do – it’s really AMAZING work!

    Can you please tell me the name of the font from my site here, or the WordPress site “BUENO” by Woopress. I have loved this font since I was a little girl – the colorful magnetic alphabet letters are in this font 😉 And do you have it available on your site? Thank you so much for taking the time to reply. I greatly appreciate it! If I can help you, even if by just sharing your link, please,let me know.

    With gratitude,

  2. WebProject says:

    Great Tutorial!I enjoyed reading it…

Comments are closed.