# Generating data from the API

## Sending the generation request <a href="#api-generation-request" id="api-generation-request"></a>

To generate data for an existing rule-based or Data Agent database:

```
POST /api/v1/generate_tasks
```

### Request body <a href="#generation-request-body" id="generation-request-body"></a>

This method expects a JSON object as the request body with the following parameters:

{% code overflow="wrap" %}

```json
{
  "workspace": string,
  "database": string,
  "format": string,
  "connection": string,
  "entity": string,
  "overrides": object
}
```

{% endcode %}

Where:

<table data-header-hidden><thead><tr><th width="233.265625" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top"><code>workspace</code></td><td valign="top">The name of the workspace in which the database resides.</td></tr><tr><td valign="top"><code>database</code></td><td valign="top">The name of the database for which to generate data.</td></tr><tr><td valign="top"><code>format</code></td><td valign="top">(Optional) The format of the generated data.<br><br>Allows values are <code>"sql"</code>, <code>"sqlite",</code> <code>"postgres-optimized"</code>, <code>"csv"</code>, or <code>"jsonl"</code>.<br><br>If you provide a connection, then do not provide a format.<br><br> For a Data Agent database, you always provide a format. You do not provide a connection.</td></tr><tr><td valign="top"><code>connection</code></td><td valign="top">(Optional) The name of the target database connection to insert the generated data into.<br><br>If you provide a format, then do not provide a connection.<br><br>For a Data Agent database, you always provide a format. You do not provide a connection.<br><br>When you provide a connection, the target database and all of the tables must already exist.</td></tr><tr><td valign="top"><code>entity</code></td><td valign="top">(Optional) The name of a table to generate.<br><br>When provided, Fabricate only generates data for that table, and the generated data is provided as plain text instead of a .zip file.</td></tr><tr><td valign="top"><code>overrides</code></td><td valign="top">(Optional) Overrides to variable values and table column configuration.<br><br>Only available for rule-based databases. Cannot use for Data Agent databases.<br><br>For more information, go to <a data-mention href="#generation-request-overrides">#generation-request-overrides</a>. </td></tr></tbody></table>

### Overriding the database configuration <a href="#generation-request-overrides" id="generation-request-overrides"></a>

{% hint style="info" %}
Only available for rule-based databases. Cannot use for Data Agent databases.
{% endhint %}

The overrides parameter is a JSON object that allows you to override variable values and column generator configuration.

Note that you cannot change the assigned generator. You can only change the configuration of the assigned generator.

For example, for the Constant generator, you change the constant value to use. Or for the Phone Number (US) generator, you can change the format.

The `overrides` parameter uses the following format:

{% code overflow="wrap" %}

```json
{
  "variables": {
    "<variable_name>": "<variable_value>"
  },
  "entities": {
    "<table_name>": {
       "<column_name>": {
           "<generator_setting_name>": "<setting_value>"
    }
  }
}
```

{% endcode %}

Where:

<table data-header-hidden><thead><tr><th width="162.15234375" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top"><code>variables</code></td><td valign="top">List of variables to override the configured variables.<br><br>For each variable, the key is the variable name, and the value is the variable value to apply.</td></tr><tr><td valign="top"><code>entities</code></td><td valign="top">A list of tables that contain columns for which to override the configuration.<br><br>Within each table, you list the columns that have overrides.<br><br>Within each column, you provide the updated configuration settings. You do not need to provide settings that are not changing.<br><br>For information about the available configuration settings for generator, go to <a data-mention href="data-model/api-column-attributes">api-column-attributes</a>.</td></tr></tbody></table>

For example, the following override changes the minimum value (`min`) of the `id` column in the `user` table.

<pre class="language-json"><code class="lang-json"><strong>overrides: {
</strong>  entities: {
    user: {
      fields: {
        id: {
          min: 10, // start the user ID primary key at 10
        }
      }
    }
  }    
</code></pre>

Be sure to set the `Content-Type` request header to "application/json".

### Returns <a href="#generation-request-returns" id="generation-request-returns"></a>

The response is a JSON object representing a data generation task. For example:

{% code overflow="wrap" %}

```json
{
  "id": string,
  "database_id": string,
  "completed": boolean,
  "error": string,
  "progress": number,
  "created_at": string,
  "updated_at": string,
  "data_url": string,
  "content_type": string
}
```

{% endcode %}

Where:

<table data-header-hidden><thead><tr><th width="250.6015625" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top"><code>id</code></td><td valign="top">The UUID of the task.</td></tr><tr><td valign="top"><code>database_id</code></td><td valign="top">The UUID of the database that the task was generated for.</td></tr><tr><td valign="top"><code>completed</code></td><td valign="top">Whether the data generation task completed.</td></tr><tr><td valign="top"><code>error</code></td><td valign="top">If an error occurred, a string that describes the error.</td></tr><tr><td valign="top"><code>progress</code></td><td valign="top">The progress of the task, as a number between 0 and 1.</td></tr><tr><td valign="top"><code>created_at</code></td><td valign="top">The date and time the task was created.</td></tr><tr><td valign="top"><code>updated_at</code></td><td valign="top">The date and time the task was most recently updated.</td></tr><tr><td valign="top"><code>data_url</code></td><td valign="top">If the task is completed, the URL from which to download the generated data.<br><br>If the request included the <code>entity</code> parameter, the data is provided as plain text.<br><br>Otherwise, the data is provided as a .zip file.</td></tr><tr><td valign="top"><code>content-type</code></td><td valign="top">The content type of the generated data.<br><br>You can use this value to determine whether the file is a .zip file.</td></tr></tbody></table>

### Example <a href="#example" id="example"></a>

For an example, go to [generate-data.js](https://github.com/mockaroo/fabricate-api-examples/blob/main/js/generate-data.js) in the `fabricate-api-examples` GitHub repository.

## Get the status of a data generation task <a href="#generation-get-status" id="generation-get-status"></a>

To check the status of a data generation task:

```
GET /api/v1/generate_tasks/:id
```

Where:

<table data-header-hidden><thead><tr><th width="124.078125" valign="top">Name</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>id</code></td><td valign="top">The UUID of the data generation task to get</td></tr></tbody></table>

The response is a JSON object in the same format as the response from the `POST /api/v1/generate_tasks` endpoint.

## Loading the generated data into your database <a href="#generation-load-data" id="generation-load-data"></a>

The `download_url` field in the response from `GET /api/v1/generate_tasks/:id` contains a URL to download the generated data.

Depending on the format of the data, to load it into your database, use the appropriate method:

#### PostgreSQL <a href="#postgres" id="postgres"></a>

The data is provided as a zip file.

To load the data into your database, unzip it and then run the following command:

```
psql -U <username> -d <database> -f path/to/dir/load.sql
```
