AutoInfer Docs

Effortlessly generate TypeScript interfaces and JSON schemas from any data source.

Getting Started

AutoInfer is a powerful CLI tool designed to streamline your development workflow. To get started, you'll need to have Node.js and npm installed. You can then install AutoInfer globally on your system.

npm install -g autoinfer

Once installed, you can access the tool from anywhere in your terminal using the `autoinfer` command.

autoinfer

Woah! Happy Building!
Our tool will guide to step by step to generate any complex TypeScript Interface / JSON Schema in seconds.

Quick Demo

See AutoInfer in action! Here's a quick demonstration of the interactive mode, inferring a TypeScript interface from a public API endpoint.

zsh

$ autoinfer

? Select data source type: › API

? Enter API endpoint URL: › https://api.publicapis.org/entries

? Select output format: › TypeScript

? Interface name: › Entries

... and a few more questions ...

✓ Output generated!

export interface Entries {
  count: number;
  entries: Entry[];
}

export interface Entry {
  API: string;
  Description: string;
  Auth: string;
  HTTPS: boolean;
  Cors: string;
  Link: string;
  Category: string;
}

Usage

AutoInfer offers two primary modes of operation to fit your needs: a user-friendly interactive prompt and a powerful direct command-line interface for automation and scripting.

Interactive Mode

For a guided experience, simply run `autoinfer` with no arguments. The tool will walk you through a series of questions to configure your schema generation.

autoinfer

The process involves these steps:

  • Select Data Source: Choose from API, JSON, CSV, or a database.
  • Provide Input: Enter the URL for an API, the file path for a local file, or connection details for a database.
  • Choose Output Format: Select either TypeScript or JSON Schema.
  • Configure Options: Set the interface/schema name, decide on optional properties, add custom fields, and more.
  • Save or Print: Choose to save the output to a file or print it directly to the console.

CLI (Flag-Based) Mode

For scripting and quick, one-off generations, you can pass flags directly to the command. This bypasses the interactive prompt.

Core Flags:

-s, --source <type>: (Required) The data source. Ex: api, json, csv.

-o, --output <format>: The output format. Ex: typescript, jsonschema. Defaults to typescript.

-i, --interfaceName <name>: The name for the main interface or schema title. Defaults to Generated.

--outFile <path>: Path to save the output file. If omitted, prints to console.

Example: Generate TypeScript from a local JSON file

autoinfer -s json -f ./data.json -o typescript -i User --outFile ./types/user.ts

Example: Generate JSON Schema from an API

autoinfer -s api -u "https://jsonplaceholder.typicode.com/users/1" -o jsonschema -i UserSchema --prettify

Data Sources

AutoInfer can infer schemas from various sources. Each source has specific flags.

API

Use the --url or -u flag to specify the API endpoint.

autoinfer -s api -u <your-api-url>

JSON

Use the --file (-f) flag for a local file or --json-input (-j) for a direct string.

# From a file
autoinfer -s json -f ./users.json

# From a string
autoinfer -s json -j '{"id": 1, "name": "Leanne"}'

CSV

Use the --file or -f flag to specify the path to your CSV file.

autoinfer -s csv -f ./data.csv

Databases (MongoDB, PostgreSQL, MySQL)

For database sources, you need to provide connection details.

# MongoDB
autoinfer -s mongodb --dbConnectionString "..." --dbName "..." --collectionName "..."

# PostgreSQL
autoinfer -s postgresql --dbConnectionString "..." --tableName "..." --dbSchema "public"

Advanced Options

Fine-tune the output with these advanced flags.

--inferOptional / --no-inferOptional

Controls whether fields that aren't present in every object of a sample array are marked as optional (e.g., name?: string). By default, this is enabled for file/API sources and disabled for SQL sources.

--prettify / -p

Formats the output with proper indentation for better readability.

--customFields <jsonstring>

Adds extra fields to the root of the generated schema. The value must be a valid JSON string.

autoinfer -s json -f data.json --customFields '[{"name":"status","type":"string"}, {"name":"tags","type":"array_string"}]'

--verbose

Enables detailed error logging, including stack traces, which is helpful for debugging.