Sign inSign up

lucascordova/api2db

By lucascordova

•Updated 6 months ago

Image
0

2.0K

lucascordova/api2db repository overview

⁠api2db

Fetches JSON from an HTTP API and inserts the raw response into a PostgreSQL table. Ideal for one-off or scheduled ETL (e.g. cron, Kubernetes CronJob).

What it does: GETs SITE_URL → parses JSON → inserts the full payload into TABLE_NAME.raw_json.

Your PostgreSQL table must already exist and have a raw_json column (e.g. TEXT or JSONB).


⁠Quick start

docker run --rm \
  -e SITE_URL="https://api.example.com/data" \
  -e DATABASE_URL="postgresql://user:password@host:5432/mydb" \
  -e TABLE_NAME="my_data" \
  your-dockerhub-username/api2db

⁠Pull the image

docker pull your-dockerhub-username/api2db

⁠Environment variables

VariableRequiredDescription
SITE_URLYesAPI URL to fetch (GET).
DATABASE_URLYesPostgreSQL connection string (postgresql://user:pass@host:port/dbname).
TABLE_NAMEYesTable name. Must have a raw_json column.
HEADER_KEYSNoJSON object of HTTP headers. Default: {}.
DEBUGNoSet to true to log a sample of the response. Default: false.

⁠Using an env file

docker run --rm --env-file .env your-dockerhub-username/api2db

Example .env:

SITE_URL=https://api.example.com/v1/records
DATABASE_URL=postgresql://user:[email protected]:5432/etl
TABLE_NAME=api_snapshots
HEADER_KEYS={"Authorization": "Bearer YOUR_TOKEN", "Accept": "application/json"}
DEBUG=false

⁠Table setup

Create the table before running the container:

CREATE TABLE my_data (
  id         SERIAL PRIMARY KEY,
  raw_json   JSONB,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

⁠Example: run on a schedule

With Docker, you can run it on a schedule (e.g. host cron):

0 * * * * docker run --rm -e SITE_URL=... -e DATABASE_URL=... -e TABLE_NAME=... your-dockerhub-username/api2db

Or use the same image as the job container in a Kubernetes CronJob.

Tag summary

Content type

Image

Digest

sha256:d0b0c5b0c…

Size

201.8 MB

Last updated

6 months ago

docker pull lucascordova/api2db