Code examples
Updated 2026-11-13
Where to find the ready-made cURL, JavaScript and Python snippets for your service, what each one does, and how to adapt it. The Excel and Google Sheets snippets have their own page.
Where the snippets are
- Open the published service.
- Go to API → Code.
- Pick cURL, JavaScript, Python, Excel or Google Sheets.
- Click Copy at the top right of the snippet.
Each snippet is written for your service: it contains the service's address, its input names and example values taken from your workbook. For a token-protected service it contains the placeholder YOUR_TOKEN_HERE and a reminder to replace it with your token.
The examples below use a service with the inputs amount and rate.
cURL
From the command line, once as GET and once as POST:
# GET: inputs as query parameters
curl "https://spreadapi.io/api/v1/services/{serviceId}/execute?amount=20000&rate=0.05"
# POST: inputs as JSON
curl -X POST "https://spreadapi.io/api/v1/services/{serviceId}/execute" \
-H "Content-Type: application/json" \
-d '{"inputs": {"amount":20000,"rate":0.05}}'For a protected service the GET address ends with &token=YOUR_TOKEN_HERE, and the POST call has the extra line -H "Authorization: Bearer YOUR_TOKEN_HERE".
JavaScript
One function for the browser and Node.js 18 or newer. It returns the results by name.
async function calculate(inputs) {
const response = await fetch('https://spreadapi.io/api/v1/services/{serviceId}/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ inputs }),
});
const data = await response.json();
if (!response.ok) throw new Error(data.message || data.error || 'Request failed');
// outputs is a list: [{ name, title, value }, ...]
return Object.fromEntries(data.outputs.map((output) => [output.name, output.value]));
}
calculate({
"amount": 20000,
"rate": 0.05
})
.then((results) => console.log(results))
.catch((error) => console.error(error.message));For a protected service the headers also contain Authorization: 'Bearer YOUR_TOKEN_HERE'. Do not ship a token in code that runs in a visitor's browser; see Calling from a web page.
Python
One function that returns the results by name. It uses the requests package.
import json
import requests
URL = "https://spreadapi.io/api/v1/services/{serviceId}/execute"
HEADERS = {}
def calculate(inputs):
response = requests.post(URL, json={"inputs": inputs}, headers=HEADERS, timeout=30)
data = response.json()
if not response.ok:
raise RuntimeError(data.get("message") or data.get("error") or "Request failed")
# outputs is a list: [{"name": ..., "title": ..., "value": ...}, ...]
return {output["name"]: output["value"] for output in data["outputs"]}
inputs = json.loads(r"""{
"amount": 20000,
"rate": 0.05
}""")
print(calculate(inputs))For a protected service HEADERS is {"Authorization": "Bearer YOUR_TOKEN_HERE"}.
Adapting a snippet
- Your values: replace the example values with your own. Keep the input names as they are.
- The token: replace
YOUR_TOKEN_HEREwith a token from API → Access. In real projects, read it from a secret store or an environment setting instead of writing it into the code. - Reading results: the JavaScript and Python functions turn the
outputslist into an object keyed by output name, soresults.interestorresults["interest"]gives one value. A cell range arrives as a table, a list of rows. - Errors: both functions raise an error with the service's message when the status is not 200. Catch it and show or log the text; see the Error reference.
- Fresh results: add
"nocache": truenext to"inputs"in the body when a call must not be answered from a stored result; see Caching and performance. - Other languages: any language that can send an HTTP request works the same way. Generate a client from the OpenAPI file if you want typed code; see OpenAPI and interactive docs.
After changing the service
The snippets follow the service. When you rename an input, add one or switch the token requirement, open API → Code again and copy the new version. Code you already copied does not change by itself; see Rename, reorder, remove.
Related
For developers and agents
- All three snippets call
https://spreadapi.io/api/v1/services/{serviceId}/execute. - POST body:
{"inputs": {...}}. Header for a protected service:Authorization: Bearer <token>. - Success is HTTP 200 with
outputsas a list of{name, title, value}. Any other status carrieserrorand usuallymessage. - A timeout of 30 seconds on the client matches the longest time a call may take.
FAQ
Where do I find example code for my service?
Open the published service and go to API → Code. Choose cURL, JavaScript, Python, Excel or Google Sheets and click Copy.
Are the snippets specific to my service?
Yes. They contain your service's address, its input names and example values from your workbook, and the token placeholder if the service is protected.
What do I replace YOUR_TOKEN_HERE with?
With a token of the service, created under API → Access. A token is shown once at creation, so use the one you stored then or create a new one.
Is there an SDK or a client library?
The service is a plain HTTP API, so no library is needed. For typed client code, generate one from the service's OpenAPI file.
How do I call the API from PHP, C# or another language?
Send a POST to the execute address with Content-Type: application/json and the body {"inputs": {...}}. Read the outputs list from the JSON answer, as the JavaScript and Python snippets do.
The JavaScript snippet works in Node.js but fails in the browser. Why?
A call from a web page follows the browser's cross-origin rules, and a token must not be placed in page code. See Calling from a web page.
I renamed an input and my code stopped working. What happened?
Copied code still sends the old name, so a required input is now missing. Copy the snippet again from API → Code or change the name in your code.
As an agent, which snippet should I base generated code on?
Use the POST form: {"inputs": {...}} to the execute address, token in the Authorization: Bearer header, results read from outputs by name. Treat any status other than 200 as an error and report the text in error.
How do I read a single result in Python?
The calculate function returns a dictionary keyed by output name. Read one value with calculate(inputs)["interest"], using your output's name.