Embed a Graphing Calculator with the Desmos API
With the Desmos API, you can place a graphing calculator on any web page and control it with JavaScript. In addition to using it in teaching materials, you can perform JavaScript-driven operations that are not possible in the regular Desmos interface and connect the calculator to other elements on the page. You can also use the API to control the regular Desmos calculator itself.
The links page collects more resources that use the API.
Working with the API
Try controlling Desmos through the API. First open Desmos, press F12 to open Developer Tools, and switch to the console. From there, you can enter JavaScript to control Desmos's expression list and graph paper.

The Desmos graphing calculator can be controlled through the Calc object. It is the GraphingCalculator returned by Desmos.GraphingCalculator(element) in the documentation, so you can use its methods for a wide range of operations.

For example, you can do the following:
// Get every expression.
Calc.getExpressions();
// Get the current graph state.
Calc.getState();
// Set a graph state.
Calc.setState(state);
// Add an expression.
Calc.setExpression({ latex: "y=x^2" });
// Update an expression.
Calc.setExpression({ id: "1", latex: "y=2x+1" });
The id and state in these examples are explained next.
About id
A Desmos graph is made up of expressions. An expression can be a formula, a note, or a table. The id is the string used to identify an expression. Desmos normally assigns consecutive IDs starting from 1, but you can assign IDs yourself through the API. When setExpression() receives an ID that already exists, it updates that expression; otherwise, it creates a new one.
About state
state is an object that represents the current state of a Desmos graph. It includes the expression list, viewport, slider values, and every other setting. Call getState() to retrieve it. For example, this is the state returned after adding (y=ax) and enabling Complex Mode:
{
"version": 11,
"randomSeed": "e8c6b8f266ea44473bbac9227ed0f18f",
"graph": {
"viewport": {
"xmin": -10,
"ymin": -13.522344013490725,
"xmax": 10,
"ymax": 13.522344013490725
},
"complex": true,
"__v12ViewportLatexStash": {
"xmin": "-10",
"xmax": "10",
"ymin": "-13.522344013490725",
"ymax": "13.522344013490725"
}
},
"expressions": {
"list": [
{
"type": "expression",
"id": "1",
"color": "#c74440",
"latex": "y=ax"
},
{
"type": "expression",
"id": "2",
"color": "#2d70b3",
"latex": "a=1",
"hidden": true
}
]
},
"includeFunctionParametersInRandomSeed": true,
"doNotMigrateMovablePointStyle": true
}
Alongside the expressions identified by id, it contains the viewport and graph settings such as "complex": true. Passing this object to setState() restores the graph to that state.
Embed a calculator in a page
You can embed a Desmos Graphing Calculator in any page and control it from JavaScript.
Get an API key
An API key is required to load the Desmos API. Open the API key page and get a key. Replace YOUR_API_KEY in this article with that key. Demo keys used to be available, but they appear to have been removed from the current documentation.
Write the HTML
Save the following as index.html, then open it in a browser. The div is where the calculator goes, the first script loads the API, and the second initializes the calculator.
<!doctype html>
<html>
<body>
<div id="calculator" style="width: 100%; height: 480px;"></div>
<script src="https://www.desmos.com/api/v1.12/calculator.js?apiKey=YOUR_API_KEY"></script>
<script>
const element = document.getElementById("calculator");
const calculator = Desmos.GraphingCalculator(element);
calculator.setExpression({
id: "parabola",
latex: "y=x^2",
});
</script>
</body>
</html>
Three details are especially important:
- Give the calculator container a height. Without one, the API can load successfully but nothing may be visible.
GraphingCalculator(element)creates the calculator. Use the returnedcalculatorto control it.- Pass expressions to
latexas JavaScript strings. In the example above,y=x^2represents (y=x^2).
To show the calculator in Japanese, add &lang=ja to the script URL and pass language: "ja" when initializing GraphingCalculator. Neither is needed for English-only use.
Add or update expressions
setExpression() adds one expression. If an expression with the same id already exists, it updates that expression. For example, replace the parabola with a line like this:
calculator.setExpression({
id: "parabola",
latex: "y=2x+1",
color: "#c74440",
});
Use setExpressions() to add several initial expressions at once.
calculator.setExpressions([
{ id: "a", latex: "a=1" },
{ id: "curve", latex: "y=a\\sin\\left(x\\right)", color: "#2d70b3" },
]);
Set the viewport
For teaching materials, you often know exactly which part of the graph should be visible at first. Pass all four bounds to setMathBounds() to set the viewport.
calculator.setMathBounds({
left: -6,
right: 6,
bottom: -4,
top: 4,
});
To prevent users from panning or zooming, also pass lockViewport: true while creating the calculator.
const calculator = Desmos.GraphingCalculator(element, {
lockViewport: true,
});
Save changes
To save the graph after a user changes an expression or the viewport, observe the change event. Checking isUserInitiated prevents the page's initial setExpression() calls from being saved as user edits.
calculator.observeEvent("change", (_name, event) => {
if (!event.isUserInitiated) return;
const state = calculator.getState();
// Use JSON.stringify(state), then save it wherever the application needs it.
});
To restore the graph, pass the saved state to setState().
References
- Desmos API Documentation — Reference for all options and methods
- Official GraphingCalculator example — The smallest working example
- Get an API key