docs/README.md Plain text
# fuyo docs fuyo runs small Ruby apps that text Swedish mobiles. An app is a web app in plain Ruby that can send and get texts. It runs on your computer and, with a paid account, on our servers. ## Start ```console $ git clone https://fuyo.sh/app.git $ ruby app/app.rb ``` The app asks what it should be called and shows its address. Open that in a browser, on the same computer or your phone. It asks you to log in to fuyo, or sign up, by texting us a word from your phone, and then whether to connect the folder to your account. Say yes, and that is all of signing up. No form, no password, no API key. The app is now running on your computer, at that address, and it can text your phone. When you want it on our servers, upgrade and push: ```console $ cd app $ git commit -am "My first change" $ git push ``` ## What is yours and what is ours `app.rb` is yours, and so is everything else in the folder, `lib/` included. `lib/` came with the starter: it is the web server and router the starter's `app.rb` uses, and it speaks the stable API for it. But it is part of your app, not of fuyo. Change it, replace it or delete it; we never change it for you, and the next app someone clones may have a different one. What it offers is described in its own comments, not here. What this file describes is what we keep working: how `app.rb` is run, what it finds around it, and the stable API. ## Contents - [Your app](#your-app): `app.rb`, and what it can count on, on your computer and on fuyo. - [Texting](#texting): whom an app texts, from which number, where the texts it gets go, and what it costs. - [The stable API](#the-stable-api): how every copy of an app talks to fuyo. ## Your app An app is a git repository with `app.rb` at its root. Everything in it is yours. This part is what fuyo does with it: how it is run, what it finds around it, and how a push puts it live. ### app.rb `app.rb` is where an app starts. On your computer you run it: ```console $ ruby app.rb ``` On fuyo we run the same command, in the root of the release, as a user with no rights to anything but the app's own files. Whatever `app.rb` does from there is up to it, as long as it: 1. **Listens for HTTP on `$PORT`**, within 30 seconds of starting. Visitors come in over plain HTTP/1.1; we take care of HTTPS. Until it listens it gets no visitors, and a new version that never does is not put live: so listen when the app is ready to answer, not before. We look by connecting to the port and closing the connection again, with no request. During a deploy the old and the new version run side by side, each on its own port, so read the port from the environment rather than writing it in. 2. **Stops on `SIGTERM`.** We send it before we stop the app, and `SIGKILL` 10 seconds later. The starter's `app.rb` does both through `lib/`, which starts its web server when `app.rb` has loaded. A minimal app of your own without it: ```ruby # app.rb require "socket" server = TCPServer.new("0.0.0.0", Integer(ENV.fetch("PORT", "8080"))) trap("TERM") { exit } loop do client = server.accept request = client.gets # "GET / HTTP/1.1" next client.close unless request # we only looked whether the app listens while (line = client.gets) && line != "\r\n"; end # the headers client.write "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 4\r\nConnection: close\r\n\r\nHej\n" client.close end ``` An app that exits is started again, a second later, then waiting longer each time it exits, up to a minute. ### What it runs on | | Your computer | fuyo | |-------------|---------------|--------------------------------------| | Ruby | yours | CRuby 4.0.10 | | Gems | what you have | only those that come with Ruby | | Files | your folder | the release (read-only), /data, /tmp | | Memory | yours | 256 MiB | | Network out | yours | TCP to ports 80 and 443, and DNS | | Output | the terminal | the app's Logs page in the dashboard | There is no `Gemfile` and no `gem install` on fuyo: a `require` of a gem that does not come with Ruby fails there. Write the app with what comes with Ruby and it runs the same in both places. The starter works on every Ruby from 2.6 (the one a Mac comes with) on. Files: - **`/data`** is the app's own, up to 1 GiB. It survives restarts and deploys. During a deploy the old and the new version both have it for a moment, so write files whole (write a new file, then rename it over the old one) or lock them. - **`/tmp`** is 32 MiB, in memory, and empty whenever the app's sandbox starts. Keep nothing there you would miss. - Everything else is read-only. Out to the internet an app can make TCP connections to ports 80 and 443 (HTTPS, HTTP) on public addresses, and ask public name servers for names. Nothing else: no email over SMTP, no private addresses. ### The environment On fuyo, an app's environment has: | Variable | What it is | |--------------------|-----------------------------------------------------| | `PORT` | where to listen for visitors | | `FUYO` | `1`: the app is running on fuyo | | `FUYO_ENVIRONMENT` | `production` | | `FUYO_DATA` | `/data` | | `FUYO_API` | where the [stable API](#the-stable-api) is | | `FUYO_TOKEN` | the app's login to the stable API | | `FUYO_COMMIT` | the commit this release was pushed as | | `SECRET_KEY` | the app's secret key | | `HOME` | `/tmp` | | `LANG` | `C.UTF-8` | | `SSL_CERT_FILE` | the certificates Ruby's OpenSSL checks servers with | Other names starting with `FUYO` are ours to use, and may come and go. `SECRET_KEY` is the key on the app's page in the dashboard, where it can be shown and changed. A release keeps the key it started with: a new key reaches the app with the next push. What the key is for is up to the app; we do not use it. `FUYO_TOKEN` sends texts as the app's account. Treat it like a password: an app that prints its environment gives it away. On your computer none of these are set unless you set them. How a copy of the app there logs in is up to its code: the starter's `lib/` asks for a token the first time it runs (see [Connecting a folder](#connecting-a-folder)) and keeps it in the folder's `.git/config` once someone has connected it. ### Visitors An app on fuyo is at `https://<address>.fuyo.dev`, where the address is six letters and digits we made up when the app was made; its page in the dashboard shows it. Per app: - at most 20 requests a second, in bursts up to 40; - at most 100 requests at once, and 1 000 open WebSockets; - a request's body is at most 10 MiB, and reaches the app with a `Content-Length`: one sent in chunks is answered `411` and never reaches it; - the app has 60 seconds to start its answer. ### Who is visiting An app can ask who its visitor is. It answers the request with `401` and a header `Fuyo-Login: required`; a browser loading a page is then sent to fuyo, to log in or sign up, and comes back to the same page. (Anything else, such as a script, gets the `401` as it is.) From then on, every request from that browser comes with what fuyo knows of them: - `Fuyo-Visitor`: `owner`, `collaborator` (one who has joined) or `stranger` - `Fuyo-Number`: the number this visitor texts the app at, and the app texts them from. Each person has their own (see [Numbers](#numbers)), so a page shows each visitor theirs. A stranger has none: the app can't text them. Without either, fuyo doesn't know who the visitor is: they haven't been asked, or have logged out of fuyo. A visitor can't send these headers themselves: fuyo takes them off every request, and so it does the cookie it keeps to know the browser by. ### Deploying ```console $ git push ``` A push to `main` puts that commit live; other branches are kept and not run. Git shows what happens as `remote:` lines, and the push waits until the new version is live or has failed. The new version starts next to the old one, on another port. Once it listens on its port, new visitors go to it, and the old one gets `SIGTERM` once the connections it still has have had a moment to finish. If the new version does not start, or exits within a minute of going live, the old one stays or comes back. The commit is kept either way, and the push says what went wrong, with the app's last lines of output. To go back to an earlier version, `git revert` and push. A push from a free account is refused, with a link to upgrade: a free account's apps run on your computer only. Until the first push, a paid account's app runs on fuyo as the starter came. ### Working on it from more than one computer The app's page in the dashboard has a clone command. A folder cloned with it is logged in from the start. Every computer the app runs on is listed on the app's page, with when it was last heard from and a button that logs it out. Collaborators (see [Collaborators](#collaborators)) work on the app too: it is in their dashboard, to clone and push. ## Texting An app texts people, and people text it back. This part is whom it can text, from which number, where the texts it gets go, and what they cost. How an app sends and gets them is the [stable API](#the-stable-api). Only Swedish mobile numbers can be texted: those starting with 070, 072, 073, 076 or 079. ### Collaborators An app texts its collaborators, and nobody else. The app's owner is its first collaborator, from the start. Anyone else the owner adds by phone number on the app's Collaborators page in the dashboard. They need a fuyo account of their own; a free one will do. An app can have up to 50. We text an invitation to each new collaborator. They join by replying YES (or JA), or say no by replying STOP (or STOPP). Until they join, the app cannot text them. Once they have: - the app can text them, and every text they send it is the app's, STOP included: what the app does with a STOP is up to the app; - they work on the app: it is in their dashboard under "Shared with you", where they can clone it and push to it; - they leave whenever they like, with the Leave button on the app's page in their dashboard, and the owner can remove them. Someone who said no, or left, cannot be invited to that app again. A collaborator who leaves or is removed is logged out of the app on all their computers. ### Numbers No app has a number of its own. We keep a pool of shared numbers, and each collaborator gets one of them for each app they are on. The app texts them from it, and what they text to it reaches the app. So two people on the same app may see it under two numbers, and one person on two apps sees each under its own. A text to a shared number from someone who is not a collaborator there is dropped. ### Where texts go The same app can be running in several places at once: on fuyo, and on the computers of whoever works on it. A text from a collaborator goes to one of them: - **On fuyo** the app gets every collaborator's texts, unless it runs on their own computer. - **On a computer** the app is logged in as the person who logged it in, gets that person's texts, and can text them. When it stops, they go back to the app on fuyo. So while you work on an app on your laptop, your texts come to your laptop, and everyone else's still go to the app on fuyo. If the app does not answer a text, it is sent again for a minute, so one that comes while the app restarts is not lost. After that the app misses it; the dashboard's Texts page has it. On a free account the app runs on your computer only, and texts only your own phone. ### What it costs | | Free | Paid | |--------------|------------------|-----------------------------| | Price | nothing | 100 kr a month | | The app runs | on your computer | there and on fuyo | | It texts | whoever runs it | that, and its collaborators | | Longest text | 3 parts | 10 parts | A text sent within Sweden costs 0,35 kr a part; a text the app gets, a tenth of that. A part is 160 characters, or 70 if the text has any character outside the GSM alphabet (most emoji, for instance); a longer text is sent as parts of 153 or 67. Everything an app texts counts against its owner's account, whoever's computer it runs on. An app on fuyo also counts 1 kr an hour of processor time and 1 kr a GiB it sends, to visitors and to the internet. Memory is not counted. Usage has two limits: one for a session of five hours, which starts with the first thing used after the last session ended, and one for the week, Monday to Monday, Swedish time. The dashboard shows how much of each is used. Past either: - a text the app sends is refused, saying when there is room again (`session_limit` or `weekly_limit`, with `resets`; see [Errors](#errors)); - a text the app gets is never refused, since it was sent already: it counts, past the limit if need be; - a paid account's apps on fuyo are paused until there is room again. Credits, bought on the Billing page, pay for what goes past the limits, at the same prices. They do not run out with time. ## The stable API It is two things: what the app asks fuyo, which is requests over HTTPS, and what fuyo sends the app, which is requests too, like its visitors'. It is called stable because an app carries its own client: we cannot change an app's code, so nothing here changes in a way an app would notice. New things come under new names. You do not need this part to build an app: the starter's `lib/` speaks the API for you. It is for knowing what happens underneath, or for writing your own client. ### Requests What is posted is a JSON object, at most 256 KiB: ``` POST https://fuyo.sh/v1/sms Authorization: Bearer <token> Content-Type: application/json {"to": "+46701234567", "text": "Hej!"} ``` The token is: - **on fuyo**, `FUYO_TOKEN` from the [environment](#the-environment); - **on a computer**, the token the folder got when it was [connected](#connecting-a-folder). The answer is JSON too. It is `200` when what was asked for was done: ```json {"sms": "m_…", "to": "+46701234567", "from": "+46766854823", "parts": 1, "status": "sent"} ``` and another status when it was not, with an `error` code to act on and a `message` for people: ```json {"error": "not_a_collaborator", "message": "+46701234567 is not one of this app's collaborators. …"} ``` A token we do not know, or no longer accept, is answered `401` and `unauthorized`. An app can ask 20 times a second, all its copies together; past that the answer is `429` and `rate_limited`. ### GET app Says what the app is. ``` GET /v1/app {"app": "quiz", "url": "https://k3x9qd.fuyo.dev", "git": "https://fuyo.sh/k3x9qd.git", "api": "https://fuyo.sh", "live": true, "paid": true, "owner": true, "phone": "+46701234567", "number": "+46766854823", "collaborators": ["+46701234567", "+46709876543"], "numbers": [], "used": {"session": 12, "week": 40}} ``` - `app`: the app's name - `url`: where it is on fuyo - `git`: its repository - `api`: the site - `live`: whether a version pushed to fuyo is running there - `paid`: whether the account pays - `owner`: whether the person at this computer owns the app; on fuyo, `true` - `phone`: the person at this computer; on fuyo, the owner - `number`: the shared number between that person and the app - `collaborators`: the collaborators who have joined, whom the app can text - `numbers`: the app's own numbers; none yet - `used`: how much of the session's and the week's limits is used, in per cent A folder that is nobody's yet (see [Connecting a folder](#connecting-a-folder)) gets only this: ``` {"unclaimed": true, "app": "quiz", "here": "https://dev-k3x9qdm2c7wa.fuyo.dev"} ``` Once someone has connected it, the answer is the app's, with `asked`, the name the folder asked for, and `new_app`, whether an app was made for it. `app` differs from `asked` when the account had an app of that name already: the new one is called `quiz-2`. ### GET tunnel On a computer the app listens nowhere: it opens a connection to fuyo, and gets its visitors and texts over it. On fuyo it listens on `$PORT` instead. ``` GET /v1/tunnel?host=kitchen-mac&os=arm64-darwin24&ruby=3.4.1 Connection: Upgrade Upgrade: fuyo-tunnel HTTP/1.1 101 Switching Protocols ``` `host`, `os` and `ruby` are the computer's name, Ruby's name for its system (`RUBY_PLATFORM`) and Ruby's version, shown with the app's computers on the dashboard. From then on both sides send frames: a number, a size, each 32 bits, big endian, and that many bytes. - From fuyo, a number and an HTTP/1.1 request, whole. - From the app, the same number and a piece of the answer, at most 1 MiB, as often as needed; then the number with no bytes: the answer is done. Answers can come in any order, and their pieces in between each other's. - From fuyo, a request's number with no bytes: the visitor has left. - Number 0 with no bytes is fuyo asking whether the app is there, every 5 seconds. Answer with the same within 5 seconds. If nothing has come for 12, the connection is gone: open another. - Number 0 with bytes, from fuyo, says why the app has to stop, for instance because the folder's app was started again. The app is at `here`, from [`GET app`](#get-app), for as long as its tunnel is up: an address of that computer's own, the same each time. Anyone who has it can open it. A tunnel is taken down when its login stops working. An app can run on 20 computers at once; the next is answered `429` and `too_many_connections`. ### Texts the app gets A text is a request to the app: ``` POST /fuyo/sms Fuyo-Id: t_… Content-Type: application/x-www-form-urlencoded from=%2B46709876543&to=%2B46766854825&text=2 ``` `from` is the collaborator, and `to` their shared number with the app. Answer with `200` and a text, and it is texted back; with an empty answer, nothing is. A reply that cannot be sent, because a limit is used up for instance, is not told to the app. Only fuyo can ask an app for anything under `/fuyo/`. A visitor who does gets a `404` from us, and the app never sees the request, so it need not check who is asking. If no answer comes, we send the same text again, with the same `Fuyo-Id`, for a minute. It may be the answer that got lost, so an app can get a text twice: one that must not act on it twice remembers the ids. An error (`500`) is an answer. After the minute the app misses the text; the dashboard's Texts page has it. A person's texts come one at a time, the next when the one before has been answered. ### Whose texts a copy gets On fuyo the app gets every collaborator's texts, but for those of a person whose own computer runs the app: they go there. If several do, the one that was started last has them. ### POST sms Texts one of the app's collaborators, from their shared number. ``` POST /v1/sms {"to": "+46709876543", "text": "Question 1 of 8: …"} {"sms": "m_…", "to": "+46709876543", "from": "+46766854825", "parts": 1, "status": "sent"} ``` `to` is a Swedish mobile number, written any usual way (`070-987 65 43` does); the answer has it as `+46…`. `from` may be left out; if given, it has to be that collaborator's shared number. `parts` is how many SMS the text took. Whom a copy may text depends on where it runs (see [Where texts go](#where-texts-go)): on fuyo, any joined collaborator; on a computer, the person who logged it in. ### Connecting a folder A copy of an app that has no token asks for one, without `Authorization`, with the name the app should have, the folder's commits, newest first (up to 50), and what computer it is: ``` POST /v1/connect {"name": "quiz", "commits": ["3f9c2e1…", "a07d4b2…"], "client": {"host": "kitchen-mac", "os": "arm64-darwin24", "ruby": "3.4.1"}} {"token": "fuyo_dev_…", "here": "https://dev-k3x9qdm2c7wa.fuyo.dev", "terms": "https://fuyo.sh/terms"} ``` `token` is the folder's from now on; send it as `Authorization`. It is nobody's yet: open the tunnel with it, and show the person `here`, the app's address, to open. Whoever opens it, logs in (or signs up) and says yes connects the folder to their account; until then the token opens a tunnel and [`GET app`](#get-app) says it is unclaimed, and nothing else. Ask `GET app` again until it is the app's. A folder nobody connects within an hour is forgotten: run again for a new token. Which app the folder is we tell by its commits: the app whose repository has the newest of them, among the person's own apps and those shared with them. With none, it is a new app. So a fresh clone of the starter is always a new app, and the same code on another computer is the same app. A name is 2 to 30 lower-case letters, digits and dashes, starting with a letter. ### Errors - `invalid_json`: the request's body is not a JSON object - `not_found`: there is nothing at that address - `unauthorized`: there is no token, or we do not know it - `rate_limited`: more than 20 requests a second; or, for `connect`, too many folders from your network - `server_error`: something went wrong on our side; try again - `invalid_to`: `to` is not a Swedish mobile number - `invalid_from`: `from` is not the collaborator's shared number - `invalid_text`: `text` is empty - `text_too_long`: the text has more parts than the account may send - `not_a_collaborator`: that number is not one of the app's collaborators - `not_joined`: they are invited, and have not replied YES - `own_phone_only`: a copy on a computer texted someone other than its person - `no_phone`: we have no phone number for the person at this computer - `session_limit`: the session's limit is used; `resets` says when there is room again - `weekly_limit`: the week's limit is used; `resets` says when there is room again - `not_activated`: a copy on fuyo, on a free account - `payment_failed`: the account's last payment failed - `canceled`: the account's subscription has ended - `suspended`: the account is suspended - `delivery_failed`: the operator did not take the text; try again - `invalid_name`: the app's name is not a name - `too_many_apps`: the account has as many apps as it can have - `too_many_connections`: the app runs on 20 computers already - `not_a_computer`: an app on fuyo asked for a tunnel - `no_apps_domain`: this server has no domain for apps ```json {"error": "weekly_limit", "message": "…", "resets": "2026-12-06T23:00:00Z"} ``` --- Questions: hej@fuyo.sh.