NPR API How-to

There were some requests on the AIR Daily maillist for a quick&dirty how-to on using NPR’s new API. Your most common use will probably be embedding a single story into your web page or blog post.

Find the story at NPR.org, then grab the url, e.g. this recent HV piece:
http://www.npr.org/templates/story/story.php?storyId=91881310

Copy the above storyID at the end of the url, and paste it into the JavaScript code below where it sez “PasteStoryIDHere”; also paste in your NPR API Key at “PasteYourAPIKeyHere” (instructions below on how to get one):

<script type="text/javascript" src="http://api.npr.org/query?id=PasteStoryIDHere,&fields=title,teaser,storyDate,show,audio&output=JS&apiKey=PasteYourAPIKeyHere"></script>

Now paste all the above into your page/post, and it’ll display like this:

To display more than one story, list each ID, separated by a comma; e.g., the ID’s of these recent Jack Chance encounters: http://www.npr.org/templates/story/story.php?storyId=89723386 http://www.npr.org/templates/story/story.php?storyId=88961745 used in this code: <script type="text/javascript" src="http://api.npr.org/query?id=89723386,88961745&fields=title,teaser,storyDate,show,audio&output=JS&apiKey=PasteYourAPIKeyHere"></script> look like this:

  

Notice I’ve specified the fields=title,teaser,storyDate,show,audio. Those fields determine what’ll show on the page: you can remove some, or add others. The list of what’s possible is under the FIELDS tab of NPR’s Query Generator.

While there, check out the many other possibilities for interacting with NPR database of stories, audio, text, and photos. You can specify search terms, NPR shows, topics, and a host of other criteria for creating custom lists. And you can see how the lists will display. If there’s demand I’ll add another how-to sometime on using the Query Generator. (UPDATE: QG how-to added.)

To get an NPR API Key, start here: http://www.npr.org/api/index

Click the upper-left Register link to start your account. Or, if you subscribe to an NPR newsletter, you already have an account: click instead the upper-left Login link and enter your NPR subscription email and password.

Once in, click the Manage link in the upper-left. Copy it. (You can get it again anytime by returning to your account settings, and using the Open API top tab.)

Update: also see post for NPR API Query Generator.

« | HV NEWS | »