wolfram-alpha-api

Classes

WolframAlphaAPI

Methods

(inner) createApiParams(baseUrl, input, outputopt) → {Promise.<FetchParams>}

Build a URL call from a baseUrl and input; specify an OutputFormat (for fetchResults).
Source:
Parameters:
Name Type Attributes Default Description
baseUrl string base URL of API we are trying to call
input string | Object string of query, or object of parameters
output OutputFormat <optional>
string which OutputFormat we want
Returns:
Type:
Promise.<FetchParams>
Example
// resolves {url: 'https://api.wolframalpha.com/v1/result?appid=DEMO&i=2%2B2', output: 'string'}
createApiParams('https://api.wolframalpha.com/v1/result?appid=DEMO', '2+2', 'string')
// resolves {
//   url: 'https://api.wolframalpha.com/v1/simple?appid=DEMO&i=nyc%20to%la&units=metric',
//   output: 'image'
// }
createApiParams('https://api.wolframalpha.com/v1/simple?appid=DEMO',
  {i: 'nyc to la', units: 'metric'}, 'image')
// rejects TypeError('method only receives string or object')
createApiParams('https://api.wolframalpha.com/v1/result?appid=DEMO')

(inner) fetchResults(params) → {Promise.<FormatParams>}

Return a Promise that downloads params.url, and resolves the results (for formatResults).
Source:
Parameters:
Name Type Description
params FetchParams
Returns:
Type:
Promise.<FormatParams>
Example
// resolves { data: '4', output: 'string', statusCode: 200,
//            contentType: 'text/plain;charset=utf-8' }
fetchResults({
  url: 'https://api.wolframalpha.com/v1/result?appid=DEMO&i=2%2B2',
  output: 'string'
})
// resolves { output: 'json', statusCode: 200, contentType: 'text/plain;charset=utf-8',
//            data: '{"queryresult" : {\n\t"success" : true, \n\t"error" : false, \n\t"nu...', }
fetchResults({
  url: 'https://api.wolframalpha.com/v2/query?appid=DEMO&input=2%2B2&output=json',
  output: 'json'
})
// resolves { output: 'image', statusCode: 200, contentType: 'image/gif',
//            data: 'R0lGODlhHAJNBfcAAAAAAAAEAAgICAgMCBAQEBAUEBgYGBgcGCAgICAkICksKSkoKTEwMT...'}
fetchResults({
  url: 'https://api.wolframalpha.com/v1/simple?appid=DEMO&i=nyc%20to%la&units=metric',
  output: 'image'
})
// resolves { output: 'image', statusCode: 501, contentType: 'text/plain;charset=utf-8',
//            data: 'Wolfram|Alpha did not understand your input' }
fetchResults({
  url: 'https://api.wolframalpha.com/v1/result?appid=DEMO&i=F9TVlu5AmVzL'
  output: 'string'
})

(inner) formatResults(params) → {Promise.<(Object|string)>}

Return a Promise that resolves a formatted form of params.data, as specified by params.output, params.statusCode, and params.contentType
Source:
Parameters:
Name Type Description
params FormatParams
Returns:
Type:
Promise.<(Object|string)>
Example
// resolves "4"
formatResults({
  data: '4', output: 'string', statusCode: 200,
  contentType: 'text/plain;charset=utf-8'
})
// resolves {success: true, error: false, numpods: 6, datatypes: 'Math', timedout: '' ...}
formatResults({
  data: '{"queryresult" : {\n\t"success" : true, \n\t"error" : false, \n\t"nu...',
  output: 'json', statusCode: 200, contentType: 'text/plain;charset=utf-8'
})
// resolves 'data:image/gif;base64,R0lGODlhHAJNBfcAAAAAAAAEAAgICAgMCBAQEBAUEBgYGBgcGCAgICAkIC...
formatResults({
  data: 'R0lGODlhHAJNBfcAAAAAAAAEAAgICAgMCBAQEBAUEBgYGBgcGCAgICAkICksKSkoKTEwMT...'
  output: 'image', statusCode: 200, contentType: 'image/gif'
})
// rejects Error('Wolfram|Alpha did not understand your input')
formatResults({
  data: 'Wolfram|Alpha did not understand your input'
  output: 'image', statusCode: 501, contentType: 'text/plain;charset=utf-8'
})

(inner) initializeClass(appid)

You may get your 'appid' at https://developer.wolframalpha.com/portal/myapps/. Remember, this appID must be kept secret.
Source:
Parameters:
Name Type Description
appid string the appid, must be non-empty string.
Throws:
TypeError
Example
const WolframAlphaAPI = require('wolfram-alpha-api');
const waApi = WolframAlphaAPI('DEMO-APPID');

Type Definitions

FetchParams

Properties:
Properties
Name Type Description
params Object
Name Type Description
url string full URL of api call
output OutputFormat which OutputFormat do we want?
Source:
Example
{url: 'https://api.wolframalpha.com/v1/result?appid=DEMO&i=2%2B2', output: 'string'}

FormatParams

Properties:
Properties
Name Type Description
params Object
Name Type Description
data string data returned by fetchResults
output OutputFormat which OutputFormat do we want?
statusCode integer HTTP status code of fetchResults contentType string HTTP content-type header from fetchResults
Source:
Example
{data: '4', output: 'string', statusCode: 200, contentType: 'text/plain;charset=utf-8'}

OutputFormat

We support four 'output' formats: 'string' and 'xml' are both strings, 'json' is an Object (a result of JSON.parse), and 'image' is a string of a "Data URI"
Properties:
Name Type Description
output 'string' | 'json' | 'image' | 'xml'
Source: