Get Started

Build your own API in Python: a private link-saver for every device

Spot a good link on your phone, save it with one tap, find it later on your laptop. Build linkstash, your own private link-saver with tags, search and an API key, using FastAPI and SQLite. It writes its own documentation, ships with tests, and finishes online behind HTTPS for your phone and automations to use.

115 minIntermediateTools: Free on your own PC. Step 7 (online) needs a small server, from $6 a month plus VAT, and a domain if you don't have one.Step 1 free
A working API with seven endpointssave, list, search, filter by tag, get, edit and delete links, stored in one SQLite file
Input checking that rejects bad data with clear errors, the right status code for every outcome, and an API key on everything private
A live documentation page at /docs where you can try every endpoint, written for you by FastAPI
  • A small web page that uses your API, and a clear understanding of CORS (the browser rule that trips everyone up)
  • Nine tests that check every route in under a second, and a server setup with HTTPS, auto-restart and nightly backups
  • A starter kit with every file, so you can compare yours or skip the typing
Before you start
01/07Set up the project and see your first /docs page0 done
STEP 01 of 07~15 min

Set up the project and see your first /docs page

uv makes a clean project folder with its own Python and packages, so nothing clashes with anything else on your PC. Two minutes later you have a live API with its own documentation page.

  1. Here's what you're building. linkstash saves web links with a title, tags and a note. Other programs (a script, a web page, an automation, your phone) talk to it over HTTP:

    • POST /links saves a link
    • GET /links lists them, with ?tag=, ?q= (search) and paging
    • GET /links/3, PATCH /links/3 and DELETE /links/3 read, edit and delete one
    • GET /health tells a monitor it's alive. Everything under /links needs your API key.
    TipShort on time? Steps 1 to 4 give you a working, key-protected API with its own /docs page in about an hour. Steps 5 to 7 (a web page, tests, going online) can wait for another evening; the starter kit has every file for them.
  2. Every file is in the starter kit: linkstash.zip (105KB). You can unzip it and follow along, or type each file as you go. Typing is slower but you'll learn more. Either way, start by making the project:

    Terminal
    uv init --bare --python 3.12 linkstash
    cd linkstash
    uv add "fastapi[standard]" pydantic-settings
  3. --bare makes just a pyproject.toml (the project's settings file) and nothing else. fastapi[standard] is FastAPI plus the extras its install guide ↗ recommends: the fastapi command, the Uvicorn web server and the httpx client. pydantic-settings is already among those extras, but you'll import it yourself, so it's listed by name. uv puts everything in a .venv folder and records exact versions in uv.lock.

  4. Open pyproject.toml in your editor and add the last two lines, so the fastapi command always knows where your app lives. The FastAPI CLI docs ↗ recommend this over typing a file name each time:

    pyproject.toml
    [project]
    name = "linkstash"
    version = "0.1.0"
    requires-python = ">=3.12"
    dependencies = [
        "fastapi[standard]>=0.143.0",
        "pydantic-settings>=2.15.0",
    ]
    
    [tool.fastapi]
    entrypoint = "main:app"
    
  5. Now the smallest possible FastAPI app, straight from FastAPI's first steps ↗. Save it as main.py in the linkstash folder:

    main.py (for now)
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    async def root():
        return {"message": "Hello World"}
    
  6. Start the development server:

    Terminal
    uv run fastapi dev
    Output
    ⚡️ Starting FastAPI in development mode
    
    🐍 Using import string: main:app
    
    🌐 Server started at http://127.0.0.1:8000
       Documentation at http://127.0.0.1:8000/docs
    
     Logs:
    
    INFO:     Will watch for changes in these directories: ['C:\\Users\\you\\linkstash']
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Started reloader process [37696] using WatchFiles
    INFO:     Started server process [26332]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
  7. Open 127.0.0.1:8000 ↗ in your browser, then 127.0.0.1:8000/docs ↗. That second page is the interactive documentation FastAPI builds from your code. It updates itself every time you add an endpoint.

  8. Two things about fastapi dev: it restarts by itself when you save a .py file, and it only listens on 127.0.0.1, so nothing else on your network can reach it. Press Ctrl + C in the terminal to stop it.

✓
CheckpointThe browser shows {"message":"Hello World"} and the /docs page lists GET /.
If it breaks'uv' is not recognised + 3 more
'uv' is not recognised
Install it as in Install any free tool from GitHub (winget install --id=astral-sh.uv -e on Windows, brew install uv on a Mac), then close the terminal and open a new one.
'Could not find a default file to run, please provide an explicit path'
You're in the wrong folder, or the file isn't called main.py. Run cd linkstash and check with dir (Windows) or ls (Mac and Linux).
'error while attempting to bind on address' (port 8000 is in use)
Something else is using port 8000, often an earlier server you didn't stop. Close the other terminal, or use another port: uv run fastapi dev --port 8001.
The page says 'This site can't be reached'
The server only runs while that terminal is open. Check it still says Application startup complete, and that the address is exactly http://127.0.0.1:8000.
Still stuck? Ask the community
Next mission · Build things with codeShip your own command-line app that anyone installs with one line
→