Quickstart
Polygen randomly generates JSON documents. You provide an input specification, describing the desired structure of your output, and Polygen will generate a document that satisfies that specification. Polygen specifications are written in a superset of JSON, extended with generators and some special forms (described below).
A simple request you can try:
$ curl https://polygen.dev -d '{ age: int:gt(17):lt(55) }'
which generates the following result:
{ "age": 32 }
Principles
<Polygen optimises for speed and simplicity. You should expect low latency responses (though we don't provide any guarantees yet). The spec language is made from a small number of building blocks that can be composed to describe arbitrarily complex documents.
API
Our API either returns verbatim JSON or a structured error (also in JSON). Any 4XX or 5XX status code should be considered an error and should be parsed as such. See the errors section for more info.
POST /api/generate
POST /
Generate a JSON document from the given spec.
Request body
{
"name": "Ada",
"age": int:gt(17):lt(55)
}
Response
{
"name": "Ada",
"age": 18
}
Errors
Errors are JSON objects with at least the keys error_type
and error_message
.
error_type
is an enum with at least the following variants:
parse_error
type_error
generate_error
internal_error
error_message
will contain more information about the error in a
human-readable format.
Status codes
Error responses use the HTTP status codes 400, 422 and 500. A 400 response indicates a problem with the spec you provided: it could not be parsed, it failed to typecheck, etc. A 422 response indicates that the spec was technically valid but we could not generate a value from it. This is usually because your guards are too strict - Polygen attempts to detect this before generation but it doesn't catch all cases. A 500 response indicates a bug in Polygen or a temporary loss of availability. You can safely retry your request after a while.
Headers
All responses from the /api/generate
endpoint will return a X-Polygen-Seed
header containing
the seed used when generating that response. You can specify this header in requests to fix
the seed, which can be useful for reproducing specific examples or debugging unexpected
output.
Reference
>> Input format
The API accepts input in a custom format which is a superset
of JSON. Any valid JSON value is a valid Polygen
input. In
addition, we support the following additional features:
- Trailing commas are permitted in arrays and objects.
- Quotes around object keys are optional.
- Generators may be specified by writing their name verbatim.
- The special forms
or
,many
andsome
.
For example, the following document is a valid Polygen
input:
{ message: string/sentences, created_at: datetime/rfc3339, id: int, visibility: or("public", "private"), }
This defines a JSON object with four fields. The field
message
will have a value populated by the generator
string/sentences
. created_at
will be populated by
the generator datetime/rfc3339
, and so on. The field
visibility
will have either the value "public"
or "private"
.
Here is an example output for this specification:
{ "created_at":"1862-11-24T19:49:31.778373274+00:00", "id":945308904754117, "message":"ducimus reprehenderit quae voluptates libero et sit iste.", "visibility":"private" }
>> Special forms
There are three special forms that can be used in the input format.
or
or("public", "private", 1, true, null, string/ascii)
or
takes one or more arguments and randomly selects one of them to use as
the output. The arguments can be any valid input element, including literal
JSON, generators and special forms.
some
some(int)
some
takes a single argument and produces an array containing one or more
elements generated from the argument. The argument can be any valid input
element.
many
many(int)
many
is like some, but it can produce an empty array.
>> Generators
Name | Description | Example |
---|---|---|
address/city | generate a city name | "Luxemburg, Luxembourg" |
colors/hex | generate a RGB hex color | "#1D3570" |
colors/hexa | generate a RGBA hex color | "#977E41C1" |
colors/hsl | generate HSL color using functional notation | "hsl(-342deg, 46%, 97%)" |
colors/hsla | generate HSL color using functional notation | "hsla(-220deg, 84%, 0%, 64%)" |
colors/name | generate a CSS color name | "plum" |
colors/rgb | generate RGB color using functional notation | "rgb(12, 228, 29)" |
colors/rgba | generate RGBA color using functional notation | "rgba(187, 106, 28, 14%)" |
country/flag | generate a country flag | "🇷🇺" |
country/iso2 | generate a country alpha-2 code | "GT" |
country/iso3 | generate a country alpha-3 code | "MUS" |
country/name | generate a country name | "North Macedonia" |
country/name/full | generate a country name - official name | "Republic of Tajikistan" |
country/uk/nin | generate a UK national insurance number | "SK480754D" |
country/undp | generate a UNDP (United Nations Development Programme) country code | "ECU" |
country/us/ssn | generate a US social security number | "493-26-0471" |
date/iso8601 | generate an ISO8601 date | "0893-02-08" |
datetime/duration | generate a string holding a duration represented as a ISO 8601 duration | "P6024Y4M8DT20H32M20S" |
datetime/rfc3339 | generate an RFC3339 datetime | "1681-10-07T16:43:08.746331208+00:00" |
datetime/unix | generate a string holding a UNIX timestamp (seconds since January 1 1970) | "6305400" |
encoding/base64 | generate a base64 encoded string | "RGF2b250ZSBHcmVlbmZlbGRlcg==" |
encoding/hex | generate a hex encoded string | "31" |
filesystem/filename | generate a filesystem filename | "deserunt_quisquam.akm" |
filesystem/filetype_ext | generate a filetype extension | "bas" |
filesystem/path | generate a filesystem path (UNIX) | "/usr/include/quo" |
float/32 | generate a 32 bit float | -2.147869451744849e38 |
float/64 | generate a 64 bit float | -2.4042637122532496e307 |
int | generate an int | 7131385927127310 |
int/even | generate an even int | 1620042622682827310 |
int/i16 | generate an signed 16 bit int | -3159 |
int/i32 | generate an signed 32 bit int | 715961504 |
int/i64 | generate an signed 64 bit int | -2146243875700030455 |
int/i8 | generate an signed 8 bit int | -32 |
int/odd | generate an odd int | -8312505006267318205 |
int/u16 | generate an unsigned 16 bit int | 63778 |
int/u32 | generate an unsigned 32 bit int | 229690509 |
int/u64 | generate an unsigned 64 bit int | 15156200289745674649 |
int/u8 | generate an unsigned 8 bit int | 119 |
internet/email | generate an email address | "judd_magnam@yahoo.com" |
internet/http_status_code | generate HTTP status code | "418 I'm a teapot" |
internet/ipv4 | generate IPv4 address | "167.166.64.210" |
internet/ipv6 | generate IPv6 address | "B6AB:CD48:B74:1B7F:EAF5:2CE6:6B58:5AE2" |
internet/mac | generate MAC address | "CB:03:44:76:A3:C9" |
internet/user_agent | generate a HTTP User Agent | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" |
person/age | generate age of a human being | 96 |
person/name | generate a person's name | "Betsy Streich" |
person/title | generate a person's formal title | "Mr" |
string | generate a UTF-8 string | "" |
string/alphanum | generate a string of alphanumeric characters | "" |
string/ascii | generate a string of (printable) ASCII characters | "" |
string/concat | concatenate an array of strings into a single string | "abc" |
string/concat_with | concatenate an array of strings into a single string, separated by the given separator | "a, b, c" |
string/emoji | generate an emoji | "🔝" |
string/names | generate names (1 or more, separated by a comma) | "Celestine Balistreri" |
string/sentences | generate sentences (1 or more, terminating in a '.' and separated by a space) | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.." |
string/words | generate words from an array of words | "lorem ipsem dolor sit amet" |
>> Guards
Some generators can be suffixed with guards, which constrain the range of
values they can produce. Guards are written directly after the generator,
separated by a colon. For example, the input
int/u8:gt(0):lt(10)
generates an integer between 1 and 9.
Name | Description | Example |
---|---|---|
lt | Set the upper bound of the generator | int/float64:lt(3.2) |
gt | Set the lower bound of the generator | int/even:gt(2) |
eq | Set the only acceptable value produced by the generator | int/odd:eq(7) |