← CSV to SQL
Your token

CSV to SQL API

Reading the CSV, choosing column types and writing the CREATE TABLE and INSERT script all run free in the browser and need no API. The API is for the one metered step: send a summary of each column of a table, and get back, as JSON, better types, NOT NULL, the primary key, unique and check rules, indexes and short notes. The page tests every suggestion against every row of the file and applies only what the data allows.

Base URL: https://api.skillsafe.ai/v1/app-api. Every response is {"data": ...} or {"error": {"code", "message"}}; read error.code and error.message rather than the HTTP status alone.

The task field

This app has one task. Always send "task": "design".

taskWhat it doesReply
designReads the column summary and suggests type changes (only types every value fits), NOT NULL, the primary key (an existing column or a new generated id), unique rules of one to three columns, checks (min, max, in, max_length), indexes and up to four notes. It never writes SQL and never sees the rows.One JSON object

Input fields

All values are strings.

FieldMeaning
task"design"
dialectpostgres, mysql or sqlite.
tableThe table name.
rowsNumber of data rows, e.g. "10".
primary_keyThe current key column, "(new id column)" or "(none)".
columnsJSON text: a list of {name, header, type, sql_type, fits, empty, not_null, distinct, unique, max_length, min, max, examples}, one per column (the page sends at most 120). fits lists, separated by |, every type id all values fit; examples (up to 5) is left out when the visitor unticks it.
columns_noteEmpty, or a sentence saying how many columns were left out.

Type ids: boolean, bigint, numeric, double, date, timestamp, timestamptz, uuid, json, text.

The reply

job.output.output is text holding one JSON object (sometimes inside a ```json fence) with six keys: columns (a list of {column, type, not_null, reason}; type may be numeric(p,s)), primary_key ({column, reason}, {add_id: true, reason} or null), unique and indexes (lists of {columns, reason}), checks (a list of {column, rule, value, reason}) and notes (0 to 4 strings). Before applying a suggestion, test it on your data: a type every value fits, NOT NULL only with no empty cell, a unique rule with no repeated value, a check every row passes. The page does all four. If the balance ran low, truncated is true and the object may stop early; the page keeps every item that arrived whole.

Worked example

Input (input.json), the Orders sample on the page (10 rows):

{
  "task": "design",
  "dialect": "postgres",
  "table": "orders",
  "rows": "10",
  "primary_key": "order_id",
  "columns": "[{\"name\":\"order_id\",\"header\":\"order_id\",\"type\":\"bigint\",\"sql_type\":\"BIGINT\",\"fits\":\"bigint|numeric|double|text\",\"empty\":0,\"not_null\":true,\"distinct\":10,\"unique\":true,\"max_length\":5,\"min\":50001,\"max\":50010,\"examples\":[\"50001\",\"50002\",\"50003\",\"50004\",\"50005\"]},{\"name\":\"customer_id\",\"header\":\"customer_id\",\"type\":\"bigint\",\"sql_type\":\"BIGINT\",\"fits\":\"bigint|numeric|double|text\",\"empty\":0,\"not_null\":true,\"distinct\":7,\"unique\":false,\"max_length\":4,\"min\":1001,\"max\":1012,\"examples\":[\"1001\",\"1002\",\"1005\",\"1007\",\"1003\"]},{\"name\":\"sku\",\"header\":\"sku\",\"type\":\"text\",\"sql_type\":\"TEXT\",\"fits\":\"text\",\"empty\":0,\"not_null\":true,\"distinct\":7,\"unique\":false,\"max_length\":9,\"examples\":[\"MUG-BLU\",\"TEE-L-BLK\",\"CAP-RED\",\"POSTER-A2\",\"TEE-M-WHT\"]},{\"name\":\"quantity\",\"header\":\"quantity\",\"type\":\"bigint\",\"sql_type\":\"BIGINT\",\"fits\":\"bigint|numeric|double|text\",\"empty\":0,\"not_null\":true,\"distinct\":5,\"unique\":false,\"max_length\":1,\"min\":1,\"max\":6,\"examples\":[\"2\",\"1\",\"3\",\"4\",\"6\"]},{\"name\":\"unit_price\",\"header\":\"unit_price\",\"type\":\"numeric\",\"sql_type\":\"NUMERIC(6,2)\",\"fits\":\"numeric|double|text\",\"empty\":0,\"not_null\":true,\"distinct\":4,\"unique\":false,\"max_length\":5,\"min\":8.75,\"max\":19,\"examples\":[\"12.50\",\"19.00\",\"15.00\",\"8.75\"]},{\"name\":\"status\",\"header\":\"status\",\"type\":\"text\",\"sql_type\":\"TEXT\",\"fits\":\"text\",\"empty\":0,\"not_null\":true,\"distinct\":4,\"unique\":false,\"max_length\":9,\"examples\":[\"shipped\",\"delivered\",\"cancelled\",\"paid\"]},{\"name\":\"ordered_at\",\"header\":\"ordered_at\",\"type\":\"timestamptz\",\"sql_type\":\"TIMESTAMPTZ\",\"fits\":\"timestamptz|text\",\"empty\":0,\"not_null\":true,\"distinct\":10,\"unique\":true,\"max_length\":20,\"examples\":[\"2024-03-01T09:15:00Z\",\"2024-03-01T11:42:10Z\",\"2024-03-02T08:05:33Z\",\"2024-03-03T19:22:00Z\",\"2024-03-04T07:58:12Z\"]},{\"name\":\"shipped_at\",\"header\":\"shipped_at\",\"type\":\"timestamptz\",\"sql_type\":\"TIMESTAMPTZ\",\"fits\":\"timestamptz|text\",\"empty\":4,\"not_null\":false,\"distinct\":6,\"unique\":false,\"max_length\":20,\"examples\":[\"2024-03-02T14:00:00Z\",\"2024-03-03T10:30:00Z\",\"2024-03-02T16:20:00Z\",\"2024-03-05T09:00:00Z\",\"2024-03-07T11:45:00Z\"]},{\"name\":\"note\",\"header\":\"note\",\"type\":\"text\",\"sql_type\":\"TEXT\",\"fits\":\"text\",\"empty\":7,\"not_null\":false,\"distinct\":3,\"unique\":false,\"max_length\":34,\"examples\":[\"gift wrap, no invoice\",\"customer changed mind\",\"office order, deliver to reception\"]}]",
  "columns_note": ""
}

Reply:

{
  "columns": [
    {"column": "unit_price", "type": "numeric(10,2)", "reason": "Prices need exact cents with room for larger amounts than the current NUMERIC(6,2)."}
  ],
  "primary_key": null,
  "unique": [],
  "checks": [
    {"column": "quantity", "rule": "min", "value": 1, "reason": "An order line has at least one item."},
    {"column": "unit_price", "rule": "min", "value": 0, "reason": "A price cannot be negative."},
    {"column": "status", "rule": "in", "value": ["shipped", "delivered", "cancelled", "paid"], "reason": "Order status is a small fixed set of states."}
  ],
  "indexes": [
    {"columns": ["customer_id"], "reason": "Orders are looked up by customer."},
    {"columns": ["status"], "reason": "Orders are filtered by their state."},
    {"columns": ["ordered_at"], "reason": "Orders are sorted and filtered by date."}
  ],
  "notes": [
    "Declare customer_id as a foreign key to a customers table once it exists.",
    "The sku and unit_price pair describes a product, so a products table would avoid repeating the price on every order line.",
    "Timestamps with a trailing Z are stored as UTC instants in timestamptz."
  ]
}

On the page, every item in this reply passes the test against the 10 rows and the script gains the checks and indexes.

Errors

HTTPWhat to do
401The token is missing, expired or a guest token on a run. Get a personal token on the token page.
402The balance is below what the run reserves. Top up at skillsafe.ai/account/billing.
400The body is not a JSON object of the fields above.
429Wait and retry with the same Idempotency-Key.

Step 1 — a tiny client helper

One function that sends JSON and unwraps the {data, error} envelope.

BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="paste-your-token-here"   # from https://csv-to-sql.skillsafe.ai/tokens.html
api() { # api METHOD PATH [JSON-BODY]
  curl -s -X "$1" "$BASE$2" -H "Content-Type: application/json" \
    ${TOKEN:+-H "Authorization: Bearer $TOKEN"} ${3:+--data "$3"}
}

Step 2 — a token

A guest token is enough for /me and /estimate. A run is metered, so it needs your personal token: sign in on the token page and copy it.

# a guest token (enough for /me and /estimate); a run needs your personal token from /tokens.html
TOKEN=$(curl -s -X POST "$BASE/guest" -H "Content-Type: application/json" -d '{"slug":"csv-to-sql"}' | jq -r '.data.token')

Step 3 — who am I, and my balance

subject_type is guest or user; credits is the balance (1 credit = $0.0001).

api GET /me | jq '.data | {subject_type, credits}' 

Step 4 — price a run for free

/estimate charges nothing and starts no job. hold_credits is the amount reserved while a run is in flight, not the price; the run is charged what it uses.

# input.json: the run input object - {"task": "design", "dialect": "postgres", "table": "orders", "rows": "10", "primary_key": "order_id", "columns": "[...]", "columns_note": ""}
api POST /estimate "$(cat input.json)" | jq '.data | {hold_credits, min_credits, model, model_alias, markup_bps, warnings}' 

Step 5 — run it and wait for the job

Send an Idempotency-Key derived from the input: a retry with the same key is never billed twice.

KEY="csv-to-sql:design:$(shasum -a 256 input.json | cut -c1-16):a1"
JOB=$(curl -s -X POST "$BASE/run" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $KEY" --data @input.json | jq -r '.data.job_id')
while :; do
  J=$(api GET "/jobs/$JOB"); S=$(echo "$J" | jq -r '.data.status')
  case "$S" in succeeded|failed|cancelled) break;; esac; sleep 2
done
echo "$J" | jq -r '.data.output.output' > reply.json   # one JSON object: columns, primary_key, unique, checks, indexes, notes
echo "$J" | jq '.data | {charged_credits, truncated}' 

Step 6 — or stream it

/run-stream sends Server-Sent Events while the job runs and the finished job at the end.

# Server-Sent Events: tick/delta events while the job runs, then a final event with the job
curl -N -s -X POST "$BASE/run-stream" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: csv-to-sql:design:$(shasum -a 256 input.json | cut -c1-16):a1" --data @input.json