Using the Wolfram|Alpha API

Todd Gayley

Wolfram Research

How to Get Started

Visit the API home page at http://products.wolframalpha.com/api/index.html.

Download the documentation PDF file

Register to use the API and obtain an AppId

API Basics

Standard Webservice API in REST style

Call a URL, get back XML

Not SOAP

Example URL

http://api.wolframalpha.com/v1/query?input=pi&appid=imuc2009

Output Formats

Output is available in several formats. You can request one or more of these format types:

Image

Plaintext

HTML

Sound

MInput

MOutput

MathML

URL Parameters

Numerous URL parameters are available for you to control the behavior of Wolfram|Alpha and the output it returns.

Pod Selection

podtitle, podindex, podscanner, etc.

Formatting

width, plotwidth, etc.

Modifying Output

assumption, podstate

Specifying Location

ip, latlong, location

Timeouts and Asynchronous Behavior

scantimeout, formattimeout, async

Using the API

To call the API from a language or programming environment, all you need is the ability to send HTTP requests and process the resulting XML.

Using the API

Tinkering in Mathematica

xml=Import["http://api.wolframalpha.com/v1/query.jsp?input=todd+as+a+given+name&appid=imuc2009","Text"]

Using the API

Tinkering in Mathematica

xml=Import["http://api.wolframalpha.com/v1/query.jsp?input=todd+as+a+given+name&appid=imuc2009","XML", "NormalizeWhitespace"->Automatic]

images=Cases[xml, XMLElement["img", {"src"->url_, __}, {}] :> url, Infinity]

Import /@ images

Using the API

Tinkering in Mathematica

text = Cases[xml, XMLElement["pod", {___, "title"->"Estimates for current US population", ___}, {___,XMLElement["subpod", _List, {___, XMLElement["plaintext", _List, {text_String}], ___}], ___}]  :> text, Infinity] // First

StringSplit[text, {"|", "\n"}] [[ 2]]

Using the API

Tinkering in Mathematica

GetNameData[name_String]:=
Module[{xml, popPod,popText, agePod,ageImage},
xml=Import["http://api.wolframalpha.com/v1/query.jsp?input="<>name<>"+as+a+given+name&appid=imuc2009&podtitle=Estimate.*","XML", "NormalizeWhitespace"->Automatic];
Quiet[
agePod=First[Cases[xml,XMLElement["pod", {___, "title"->"Estimated current age distribution", ___}, _List], Infinity]];
popPod=First[Cases[xml,XMLElement["pod", {___, "title"->"Estimates for current US population", ___}, _List], Infinity]];
ageImage=Import[First[Cases[agePod,XMLElement["img", {"src"->url_, ___}, _List]:>url, Infinity]]];
popText=First[Cases[popPod,XMLElement["plaintext", _List, {text_String}]:>text, Infinity]];
popText=StringSplit[popText, {"|", "\n"}][[2]];
];
If[MatchQ[{popText, ageImage}, {_String, _Image}],
{popText, ageImage},
(* else *)
{"No data available", ""}
]
]

GetNameData["Todd"]

Using the API

Tinkering in Mathematica

Manipulate[Grid[{{Style["Expected number\nalive in US:",FontFamily->"Times"],Style[#1,FontFamily->"Dialog"]},{Style["Age distribution:",FontFamily->"Times"], Rasterize[#2]}}, Alignment->{Left, Center}, Spacings->2]& @@ GetNameData[ToString[name]], {{name, Todd, Style["Given Name:", Bold]}}]

Clients of the API

iPhone App

Mathematica 8

Language Bindings

Language bindings are available in several popular languages, with many more to come.

These libraries of code simplify the use of the Wolfram|Alpha API from a given language.

Created by members of the developer community. Contribute your own!

Visual Basic .NET Sample Code

Adapted from .NET Binding, author Mathy Lorenzen

Engine.LoadResponse(WolframAlphaSearchTerms, WolframAlphaApplicationID)

For Each Item As WolframAlphaPod In Engine.QueryResult.Pods

   Output("Sub pods found: " & Item.NumberOfSubPods, 2, ConsoleColor.White)
   Output("Title: " "" & Item.Title & "" "", 2, ConsoleColor.White)
   Output("Position: " & Item.Position, 2, ConsoleColor.White)

   For Each SubItem As WolframAlphaSubPod In Item.SubPods
      Output("Title: " "" & SubItem.Title & "" "", 3, ConsoleColor.White)
      Output("Pod text: " "" & SubItem.PodText & "" "", 3, ConsoleColor.White)
      Output("Pod image title: " "" & SubItem.PodImage.Title & "" "", 3, ConsoleColor.White)
      Output("Pod image width: " & SubItem.PodImage.Width, 3, ConsoleColor.White)
      Output("Pod image height: " & SubItem.PodImage.Height, 3, ConsoleColor.White)
      Output("Pod image location: " "" & SubItem.PodImage.Location.ToString & "" "", 3, ConsoleColor.White)
   Next

Next

Widgets

Widgets are a layer on top of the API

Rapid development for single-topic embeddable interfaces

Coming soon

Future Directions

New features, format types

Raw data access ("Data Exposure API")

Deeper Mathematica Integration