Call REST and GraphQL APIs
Four statements connect a workflow to an API. Each one is a line in a formula node’s node file:
| Statement | What it does |
|---|---|
fromApi(url) |
Calls a REST API and brings its answer in as rows |
toApi(url) |
Sends rows to a REST API; the rows pass through |
fromGraphql(url, query) |
Runs a GraphQL query and brings its records in as rows |
toGraphql(url, mutation) |
Sends rows through a GraphQL mutation; the rows pass through |
The Learn connecting workflows to APIs and email catalog has a runnable lesson for each: 01 Read from a REST API, 02 Send rows to a REST API, and 03 Query a GraphQL API. Clone the Workspace, then press Run. The lessons call free public test APIs that need no key.
One call, or one call per row
Section titled “One call, or one call per row”Without a subject, a statement makes one call for the whole node. Put eachRow() in front to make one call per row:
# One call: the answer becomes the node's rows.fromApi("https://api.frankfurter.dev/v1/latest") .query("base", "EUR") .returns("date", "text") .returns("rates", "any")# One call per row: the answer's fields are added to that row.eachRow().fromApi("https://api.frankfurter.dev/v1/{day}") .path("day", prop("day")) .query("base", prop("ccy")) .records("rates") .returns("EUR", "number")With eachRow(), a call that fails leaves its returned columns empty on that row and records the reason on the row; the other rows still get their answers. A one-call statement that fails fails the node. Add .oneToMany() when one row’s call returns a list and each item should become a row of its own.
Read from a REST API
Section titled “Read from a REST API”fromApi(url) sends a GET unless you set .method(...). Shape the request with:
| Option | Use |
|---|---|
.method("GET" | "POST" | "PUT" | "PATCH" | "DELETE") |
The HTTP method |
.path(name, value) |
Fills a {name} placeholder in the URL |
.query(name, value) |
Adds a query parameter |
.header(name, value) |
Adds a request header |
.body(value), .bodyField(name, value), .bodyJson(text) |
The request body: one value, one field at a time, or a JSON document written as text |
.records(path) |
Where the list sits in the answer, dot-separated, such as "data.items"; leave it out when the answer is a list or one object |
.returns(name, type) |
One line per field to keep, with its type: text, number, date, boolean, or any; without .returns every field is kept |
.timeout(seconds), .retries(n) |
How long one request may take, and how often a failed one is retried |
.numRowsToProcess(n), .offset(n) |
How many rows a per-row call sends, from which row; -1 is every row |
.maxResponseBytes(n) |
The largest answer accepted |
A value sent to an API is a literal, a secret("NAME"), or one column with prop("name") (per-row calls only). To send anything computed, write it into a column with setProp first:
setProp("day", formatDate(prop("value_date"), "YYYY-MM-DD"))eachRow().fromApi("https://api.example.com/rates/{day}") .path("day", prop("day"))An answer field that holds an object arrives as one column; prop("rates").prop("USD") reads a field inside it.
Send rows to a REST API
Section titled “Send rows to a REST API”toApi(url) sends rows and passes them on, so the workflow carries on after the call.
# One POST per row; the id the API answers with is added to the row.eachRow().toApi("https://api.example.com/orders") .method("POST") .send("order_id", "customer", "total") .returns("id", "number").send(name, …) names the columns that make the JSON body; .send() with no names sends every column. Without eachRow() the rows go out as one JSON list, split into requests of .batch(n) rows. A whole-table send keeps no answer: when you need what the API returns, send per row.
Query a GraphQL API
Section titled “Query a GraphQL API”fromGraphql(url, query) posts the query and reads the records under the answer’s data. Write the query between backticks so it can span lines:
fromGraphql("https://countries.trevorblades.com/", `query ($continent: String!) { countries(filter: { continent: { eq: $continent } }) { code name capital }}`) .variable("continent", "OC") .records("countries") .returns("code", "text") .returns("name", "text") .returns("capital", "text").variable(name, value) sets a query variable. With eachRow(), a variable can read the row, .variable("code", prop("country_code")), and .send("code") is the short form when the column and the variable have the same name. A response that carries GraphQL errors fails that call and shows the messages.
Send rows through a GraphQL mutation
Section titled “Send rows through a GraphQL mutation”toGraphql(url, mutation) takes the same options:
eachRow().toGraphql("https://api.example.com/graphql", `mutation ($title: String!) { createPost(input: { title: $title }) { id }}`) .send("title") .records("createPost") .returns("id", "text")Read every page of a paged API
Section titled “Read every page of a paged API”A paged API returns a slice of the set and a token, URL, or page number for the rest. Add .paginate(...) to fromApi or fromGraphql, and the call follows those pages until the API says there are no more; the node gets every record, in order.
# A Notion database query: 100 entries per page, next_cursor for the rest.fromApi("https://api.notion.com/v1/databases/<id>/query") .method("POST") .header("Notion-Version", "2022-06-28") .auth(bearer(secret("NOTION_TOKEN"))) .records("results") .paginate(cursor("next_cursor", "start_cursor"))| Style | What you write | First request | Stops when |
|---|---|---|---|
| Cursor | .paginate(cursor("next_cursor", "start_cursor")) |
Sends no cursor | The response field is missing, null, or empty text |
| Next URL | .paginate(nextUrl("links.next")) |
The call’s URL | The response field is missing, null, or empty text |
| Page | .paginate(page("page")), or page("page", 0) for an API that counts from 0 |
Sends 1, or the start you set | A page has no records |
| Offset | .paginate(offset("offset")) |
Sends no offset | A page has no records |
- One call for the whole node:
.paginategoes on a call without a subject, or withinputs("n"); not oneachRow()orcontext("n"). - Where the paging field goes: a top-level field of the JSON body for
POST,PUT, andPATCH(a call with no body sends just that field), the query string forGETandDELETE, and the variables forfromGraphql. A field the call already sets is an error. Response fields are dot paths, read insidedatafor GraphQL, as.recordsis. - Offset: steps by the number of records each page returned. Set the page size on the call itself, such as
.query("limit", 100)..offset(n)on the call is a different option: it skips input rows. - Next URL: may be relative, and resolves against the call’s URL. The next request is that URL as given, with the call’s method, headers, sign-in, and body. A next URL on another origin fails the call, so your credentials stay with the API you named.
- Limits: every page counts against
.maxResponseBytes,.timeout,.retries, and the run’s cap of 1,000 requests and 50 MiB. A page that fails after its retries, a repeated cursor or next URL, or a limit reached part-way fails the whole call, and the node keeps no partial set.
Picking an operation from an OpenAPI specification does not set paging; add .paginate yourself.
Sign in to an API
Section titled “Sign in to an API”Store the credential as a workspace secret and name it in .auth(...). The value never appears in the node file:
| Sign-in | Statement |
|---|---|
| Bearer token | .auth(bearer(secret("CRM_TOKEN"))) |
| User and password | .auth(basic("svc-user", secret("CRM_PASSWORD"))) |
| API key in a header | .auth(apiKey(secret("CRM_KEY")).header("X-API-Key")) |
| API key in the query | .auth(apiKey(secret("CRM_KEY")).query("api_key")) |
| OAuth 2 client credentials | .auth(oauth2("https://login.example.com/token", "client-id", secret("CRM_CLIENT_SECRET")).scope("read")) |
| Login form | .auth(formLogin("https://app.example.com/login", "svc-user", secret("APP_PASSWORD"))) |
Credential headers such as Authorization and Cookie are set by .auth, not by .header.
Outbound access
Section titled “Outbound access”A call goes out from your deployment’s workflow engine. If your platform team restricts outbound traffic, the hosts a workflow calls must be allowed; a call to a host that is not allowed fails with the connection error.
Related
Section titled “Related”- Formula: the language, from loading to exporting
- Formula node: node files and every statement
- Workspace secrets: storing credentials
- Send and receive email
- Learn by example: every seeded lesson