API - axkr/symja_android_library GitHub Wiki

JSON API Server

An API is available through the SymjaServer.java which is implemented as a undertow server.


Installation

Make sure a Java 11 runtime (JRE 11) is installed. At a terminal you can type java -version to see if it is installed.

Download the latest Symja release from

Unzip the download in a separate folder and modify the jsonapi-server.bat file to use your Java 8 installation path to run the Symja JSON API Server.

"%JAVA_HOME%\bin\java" -Dfile.encoding=UTF-8 -classpath "lib/*" org.matheclipse.api.SymjaServer 

If you start this server

JSON API startup

you can for example evaluate D(Sin(x),x) with the URL

and get a response in JSON format.

{
  "queryresult" : {
    "success" : "true",
    "error" : "false",
    "numpods" : 3,
    "version" : "0.1",
    "pods" : [ {
      "title" : "Input",
      "scanner" : "Identity",
      "error" : "false",
      "numsubpods" : 1,
      "subpods" : [ {
        "plaintext" : "D(Sin(x),x)",
        "sinput" : "D(Sin(x),x)",
        "latex" : "\\frac{d}{{dx}}\\sin (x)"
      } ]
    }, {
      "title" : "Derivative",
      "scanner" : "Derivative",
      "error" : "false",
      "numsubpods" : 1,
      "subpods" : [ {
        "plaintext" : "Cos(x)",
        "sinput" : "D(Sin(x),x)",
        "latex" : "\\cos (x)"
      } ]
    }, {
      "title" : "Alternate form",
      "scanner" : "Simplification",
      "error" : "false",
      "numsubpods" : 1,
      "subpods" : [ {
        "plaintext" : "1/(2*E^(I*x))+E^(I*x)/2",
        "sinput" : "TrigToExp(Cos(x))",
        "latex" : "\\frac{1}{2\\,{e}^{i \\,x}}+\\frac{{e}^{i \\,x}}{2}"
      } ]
    } ]
  }
}

Maven

Alternatively, if you're using Maven you can run this Maven command:

mvn -f pom.xml exec:java@api -pl matheclipse-api

Query Parameters

The following request parameters are available. The input parameter i must be available exactly once. The format parameter f can be set multiple times.

  • i: the expression which should be evaluated in URL encoded form
  • f: plaintext - returns the result in plaintext or markdown format
  • f: latex - returns the result in latex format
  • f: mathml - returns the result in mathml format
  • s: true (any value) - strict Symja expression parsing, if omitted or not set a "fuzzy parser" was used to interpret the input expression

See the OpenAPI specification for symja.org


Simple client example

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SymjaClientExample {
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    // JSON URL to Java object
    try {
      JsonNode node =
          mapper.readTree(
              new URL(
                  "http://localhost:8080/v1/api?i=D(Sin(x)%2Cx)&f=latex&f=plaintext&f=sinput&appid=DEMO"));
      System.out.println(node.toPrettyString());
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}