Appearance
Syncs
A sync asks another system for data at a set time and writes it into a collection of your app: a franchise system's daily figures every night, exchange rates every morning, orders from a shop. You set it up once and it runs by itself.
It is built for numbers that have to be trusted. Running it twice never adds anything twice, a figure that is corrected later is kept as a new revision with the first figures still in a history, and a day that never arrived shows as a gap rather than as zero.
Open it from Syncs in the app admin, under API access. (A sync pulls data in. To let another program read your data, see API Access. To receive data pushed to you, see the webhooks on a collection's page.)
Set up a sync
Click New sync, give it a name, and work down the six steps.
1. Where the data comes from
Address: the web address to ask. The sync makes a
GETrequest to it.Which days to ask for: a Start date and an End date, each counted back from today (0 is today, 1 is yesterday). Start 7 and End 1 asks for the last seven days up to yesterday. The box beside them shows the real dates for today. Asking for more than one day is what lets a figure corrected later be noticed, because the same days are asked for again and compared.
Headers and Query parameters: anything the other system needs, such as an API key or a date range. For each one, type the name and pick what the value is:
Value is What is sent Text Exactly what you type. A fixed date typed here never moves. Secret A value kept on the server, such as an API key. It is never shown again after you save it. Start date, End date, Today The matching date, worked out each time it runs. A field of the record A value from the record being asked about (see the next switch). Ask separately for each record of a collection: for a system that answers one location at a time. Pick the collection (for example Locations) and, if you like, a filter ("only those where Status is active"). The sync then asks once for every one of them, and a request can use that record's own values. If one fails, the others still go through.
2. What comes back
Press Fetch a sample. The sync asks the address once and shows the real answer, finds the list of items in it, and shows what each item holds. You can open The answer, as it came back to read it yourself. If you cannot fetch yet, paste an example answer from the other system's documentation.
The other system must answer with JSON. If it does not, the sample says so and shows how the answer begins; many systems need a parameter such as format=json for this.
3. Where it goes
Pick the collection to fill. Each of its fields gets a dropdown of the values that came back. Match by name fills the obvious pairs. A field can also take a value from the record being asked about, or a fixed text. Leave a field empty to leave it alone.
4. Name each record
Each item becomes a record with a name made from its own values, such as the location and the day. The same name is always the same record, which is why running again never adds anything twice. Pick values that tell records apart. If two items in one answer would get the same name, the second is left out and the run reports it, so make the name more specific.
5. Keep it safe
Two optional switches, each filled in from your field names:
- Keep a history of corrections. When a figure arrives again with a different value, the record is updated, its revision goes up, and a row with the new figures is added to a history collection of your choice. The first figures are never lost. The history collection needs a reference field pointing back at the record, and a number field for the revision is a good idea.
- Show a missing day as a gap. If a day the sync expected does not come back, a record for it is created and marked missing, and it fills in when the figures arrive later. This needs a Date field filled from the answer and a field that can hold the state, such as a Status choice. It suits data that arrives every day. Leave it off for data that does not, such as earthquakes.
6. When it runs
Every day at a time and time zone you pick, every hour, or only when you press Run now. Try it asks one record and shows what the sync would do, without writing anything. Use it before you save.
Watching your syncs
The Syncs page lists each sync with its status, last result and next run. A red strip at the top appears when a sync did not finish cleanly the last time. Press Run now to start one, and the clock icon to see its run history: each run's time, whether it ran on schedule or by hand, and what it did.
| In the run history | Means |
|---|---|
| new | Records created for the first time (revision 1). |
| corrected | Records whose figures changed. The new figures were saved and the old ones kept in the history. |
| unchanged | Records that came back exactly as already held, so nothing was written. |
| gaps | Days written as missing. |
| repaired | Records whose history row was missing (a run that stopped partway) and has now been written. |
| problems | Items or requests that failed, listed in words under the run. |
A request that fails leaves that record's days exactly as they were, and the others carry on. The last 60 runs of each sync are kept.
Good to know
- A sync writes as the app's admin account, so a role that can only see its own records sees a synced record only if it is assigned to them.
- Records are checked against your fields like an import is: numbers must be numbers, dates must be dates, choices must be one of the options. An item that does not fit is reported and the rest still go through.
- Addresses on private networks (such as
localhostor192.168.x.x) are refused. - A request that is not answered within 30 seconds is given up on and reported.
- Limits: 500 records to go through, 2,000 items in one answer, and a start date up to 60 days back.
- Saved secret values stay on the server. A sync can only use collections and fields that have been saved to the app, so save the schema first.
A worked example: earthquakes
The U.S. Geological Survey publishes earthquakes with no key needed, and revises them after the fact, which makes it a good test. In a collection with a place, magnitude, time and status:
- Address
https://earthquake.usgs.gov/fdsnws/event/1/query. Query parameters:formatas Textgeojson,starttimeas Start date,endtimeas End date,minmagnitudeas Text4.5. Start date 7, End date 1. - Fetch a sample. It finds the list
features. - Match the fields (
properties.place,properties.mag,properties.time,properties.status, andidfor an event id). - Name each record by the event id.
- Turn on Keep a history of corrections.
- Run it. Run it again and nearly everything is unchanged. When USGS revises an event, it appears as revision 2 with its first figures still in the history.
When something goes wrong
| You see | Why |
|---|---|
The answer is not JSON. It starts with <?xml… | The other system needs to be asked for JSON, often with a format parameter. |
| There is no list of items in this answer. | The answer holds no list of objects to turn into records. |
| Two items have the same record name… | Make the record name more specific. |
| The other side answered 401 | Check the key or header. 404 means check the address. |
| Missing days needs a Date field… | Match a date from the answer first, or turn missing days off. |
| This target URL is not allowed | The address points at a private network. |
