Connect your own API
If your product already has a web API, your Ynch app can read and write through it. You don’t need a database connection, and you don’t change your API. You describe each endpoint your app uses, or import them from an OpenAPI document, and Studio calls them for the phone.
- The phone talks only to Ynch. Ynch calls your API over https, at the address you saved in Studio.
- Your app’s files hold relative paths and field names. They never hold your API’s address or a key.
- People sign in with their own account on your API. Their session stays on their phone and is sent only to your API. Ynch stores no password and no session.
- Nothing runs until you accept it. After you make a version live, you see every endpoint it calls.
From a terminal
Section titled “From a terminal”ynch initynch api connect https://portal.example.comynch api import openapi.jsonynch checkynch upload --activate --acceptynch initmakes the project and connects this computer to Studio. Approving the computer in your browser is the only step outside the terminal.ynch api connectsaves your API’s address after testing that it answers. If your API wants the same key on every request, add--header x-api-key; you type the value at a hidden prompt and it is stored encrypted. If you later move the project to a different address, the command asks first, wants the header value typed again, and sign-in inside your app waits until you accept it for the new address.ynch api importreads an OpenAPI 3 document (a file or an https address, JSON or YAML) and writes model and operation files. It tells you what it wrote, what it skipped and why, and what to review. Files you already have, and a sign-in you already declared, are kept unless you add--force.- Build your screens on those operations, as in Models and operations.
ynch upload --activate --acceptbuilds, signs and makes the version live, then lists each endpoint it will call and asks before accepting them. In a script, add--force.
You can do the same in Studio: connect under Database, choose Your own API, and press Accept changes after making a version live.
The connection
Section titled “The connection”.ynch/connections.json says the app uses an API and how people sign in to it:
{ "schemaVersion": "1.0.0", "connections": { "primary": { "cloudConnectionId": "conn_portal", "provider": "api", "auth": { "login": { "path": "/api/v1/login", "emailField": "email", "passwordField": "password" }, "refresh": { "path": "/api/v1/refresh", "tokenField": "refreshToken" }, "session": { "token": "/accessToken", "renewal": "/refreshToken", "expiresIn": "/expiresIn", "userId": "/user/id", "email": "/user/email" } } } }}loginis aPOSTwith a JSON body.emailFieldandpasswordFieldare the names your API expects, so an API that takesusernameis"emailField": "username".sessionsays where things sit in the reply, as JSON pointers:/accessTokenis the top-level keyaccessToken;/data/tokenistokeninsidedata. Onlytokenis required.refreshis optional. Without it, a person signs in again when their session ends.- The token is sent as
Authorization: Bearer <token>. For another header, add"send": { "header": "x-session-token" }. - Leave
authout if your API has no sign-in. Then every request carries only the fixed header, if you saved one.
Operations
Section titled “Operations”An API operation is like a database one, with request and response in place of a table:
{ "schemaVersion": "1.0.0", "id": "list-projects", "kind": "list", "connection": "primary", "connector": "api", "responseModel": "project", "columns": ["id", "name", "client", "status", "start-date", "spent"], "parameters": { "q": { "type": "string", "required": false, "maxLength": 80 }, "status": { "type": "string", "required": false, "maxLength": 20 } }, "filters": [], "search": { "parameter": "q" }, "pagination": { "mode": "offset", "defaultLimit": 25, "maxOffset": 10000 }, "maxRows": 100, "request": { "method": "GET", "path": "/api/v1/projects", "queryNames": { "q": "search", "status": "status" }, "pagination": { "style": "offset", "offset": "offset", "limit": "limit" } }, "response": { "rows": "/data", "hasMore": "/meta/hasMore", "fields": { "spent": "/totals/spent" } }}{ "schemaVersion": "1.0.0", "id": "update-project", "kind": "update", "connection": "primary", "connector": "api", "responseModel": "project", "columns": ["id", "name", "client", "status", "start-date", "spent"], "parameters": { "id": { "type": "integer", "required": true, "minimum": 1 }, "name": { "type": "string", "required": true, "minLength": 1, "maxLength": 80 }, "start-date": { "type": "date", "required": false } }, "values": { "name": "name", "start-date": "start-date" }, "filters": [{ "field": "id", "operator": "eq", "parameter": "id" }], "maxRows": 1, "request": { "method": "PATCH", "path": "/api/v1/projects/{id}", "body": { "start-date": "/startDate" } }, "response": { "record": "" }}Where each parameter goes. Every parameter is sent exactly once:
- in the path, as
{id}. It must be a required parameter; its value is encoded as one path segment. - in the query, through
queryNames(your parameter, then the name your API uses). - in the body, for a parameter that
valueswrites. The key is the field’s name with underscores (start_date) unlessrequest.bodygives a pointer.
filters says which parameter picks the row (always eq), and it must be in the path or the query. get,
update and delete need one on the model’s identity field. The one
exception is a get marked "single": true, such as GET /api/v1/dashboard or GET /api/v1/me: it names no id and
reads the one record your API returns for the signed-in person. ynch api import reads an endpoint like that as a
single get, with a note for you to check.
Optional filters. On a list, an optional parameter that has no value is left out of the query. That is how an
“All” chip shows everything: your API sees no status at all.
Letting the person choose the order. A list can name the orders a SortMenu
offers in sortOptions (see What a person can choose). Your API
sorts, so request.sortNames says which query name to send and your API’s own value for each order:
{ "sortOptions": { "name": [{ "field": "name", "direction": "asc" }], "newest": [{ "field": "start-date", "direction": "desc" }] }, "request": { "method": "GET", "path": "/api/v1/projects", "sortNames": { "parameter": "sort", "values": { "name": "name", "newest": "-startDate" } } }}values must give exactly the names in sortOptions, and the query name can’t be one the operation already sends.
Studio sends only a name the approved operation lists; anything else is refused before it reaches your API.
Paging. request.pagination is {"style": "offset", "offset": "...", "limit": "..."} or
{"style": "page", "page": "...", "size": "..."} with your API’s own names (pages count from 1; add
"firstPage": 0 if yours count from 0). Leave it out if your API returns everything at once; the list then has one
page.
Reading the reply.
response.rowspoints at the list;""means the reply itself is the list.response.recorddoes the same for one record.response.hasMorepoints at a true or false value. Without it, a full page means there may be more.- A field is looked up under its own name, then with underscores, then in camelCase:
start-datefindsstart_dateorstartDate. Anything else, or a nested value, gets a pointer inresponse.fields. - Only the
columnsyou list reach the phone, and each must fit its model field. A reply that doesn’t fit is an error on the phone, never a wrong value.
| Kind | Method | Reply |
|---|---|---|
list |
GET |
rows |
get |
GET |
record |
insert |
POST |
record, if your API returns the saved record |
update |
PATCH or PUT |
record, if your API returns it |
delete |
DELETE |
none |
What your API should answer
Section titled “What your API should answer”| Your API says | The app shows |
|---|---|
| 200 to 299 | The rows, or “saved” |
| 401 | The sign-in screen (after one try at renewing the session) |
| 403 | “This operation is not permitted.” |
| 404 | “The record was not found.” |
| 409 | A conflict message |
| 429 | “Too many requests. Try again later.” |
| Anything else | A general error. Your API’s own error text is never shown. |
Limits
Section titled “Limits”- https only, on the standard port, at a host name. No redirects.
- JSON in and out. A reply over 1 MB, or one that takes more than 8 seconds, is an error.
- Email (or user name) and password sign-in. Not yet: sign-in with another service, one-time codes, cursor paging, file uploads sent to your API, and pictures served from your own host. Photos and files stored with Ynch work as they do with a database: your API stores the ID.
- One API per project.
The Northline Portal example is a complete app on an API, with the API itself and its OpenAPI document.