---
seo_title: API TOML in JSON | Converti file TOML in JSON | EnConvert
meta_desc: Converti TOML in JSON con POST /v1/convert/toml-to-json. Carica un file .toml e ricevi JSON formattato direttamente o tramite un URL di download prefirmato.
keywords: toml in json api, convertire toml in json, api conversione toml json, toml to json rest api, parsare toml in json api, toml in json python, toml in json curl, file di configurazione toml in json api
---

# API TOML in JSON

L'API TOML in JSON converte un file TOML in formato JSON con una singola richiesta a `POST /v1/convert/toml-to-json`. Carica un file `.toml` tramite multipart form data e ricevi direttamente JSON formattato con indentazione a 2 spazi, oppure imposta `direct_download=false` per ottenere i metadati con un URL di download prefirmato. I valori datetime TOML vengono preservati come stringhe ISO-8601.

---

## Endpoint

```
POST /v1/convert/toml-to-json
```

**Content-Type:** `multipart/form-data`

**Input accettato:** file `.toml` (codificati in UTF-8)

**Formato di output:** `.json` (`application/json`)

---

## Autenticazione

Richiede una chiave API privata oppure un token JWT ottenuto da una chiave pubblica.

```
X-API-Key: sk_your_private_key
```

Oppure:

```
Authorization: Bearer <jwt_token>
```

---

## Parametri della richiesta

| Parametro | Tipo | Obbligatorio | Predefinito | Descrizione |
|-----------|------|----------|---------|-------------|
| `file` | file | Sì | -- | Il file `.toml` da convertire. Deve essere codificato in UTF-8. |
| `output_filename` | `string` | No | Nome file di input | Nome file di output personalizzato. L'estensione `.json` viene aggiunta automaticamente. |
| `direct_download` | `boolean` | No | `true` | Con `true`, restituisce i byte JSON grezzi. Con `false`, restituisce i metadati con un URL di download prefirmato. |

---

## Regole di conversione

Mappatura diretta da TOML a JSON:

```toml
[database]
host = "localhost"
port = 5432

[database.credentials]
user = "admin"
password = "secret"

features = ["auth", "logging"]
```

Diventa:

```json
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "credentials": {
      "user": "admin",
      "password": "secret"
    }
  },
  "features": ["auth", "logging"]
}
```

- Le tabelle TOML diventano oggetti JSON
- Gli array TOML diventano array JSON
- I valori datetime TOML vengono serializzati come stringhe ISO-8601
- L'output è indentato con 2 spazi e l'unicode viene preservato

---

## Risposta

### Download diretto (`direct_download=true`, predefinito)

```
HTTP 200 OK
Content-Type: application/json
Content-Disposition: inline; filename="config_20260405_123456789.json"
```

### Risposta con metadati (`direct_download=false`)

```json
{
    "presigned_url": "https://spaces.example.com/...",
    "object_key": "env/files/{project_id}/toml-to-json/config_20260405_123456789.json",
    "filename": "config_20260405_123456789.json",
    "file_size": 1234,
    "conversion_time_seconds": 0.03
}
```

---

## Esempi di codice

### Python

```python
import requests

with open("config.toml", "rb") as f:
    response = requests.post(
        "https://api.enconvert.com/v1/convert/toml-to-json",
        headers={"X-API-Key": "sk_your_private_key"},
        files={"file": ("config.toml", f, "application/toml")}
    )

with open("config.json", "wb") as out:
    out.write(response.content)
```

### Node.js

```javascript
const form = new FormData();
form.append("file", fs.createReadStream("config.toml"));

const response = await fetch("https://api.enconvert.com/v1/convert/toml-to-json", {
    method: "POST",
    headers: { "X-API-Key": "sk_your_private_key" },
    body: form
});

const json = await response.json();
```

### PHP

```php
$ch = curl_init("https://api.enconvert.com/v1/convert/toml-to-json");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ["X-API-Key: sk_your_private_key"],
    CURLOPT_POSTFIELDS => ["file" => new CURLFile("config.toml")]
]);
$json = curl_exec($ch);
curl_close($ch);
```

### Go

```go
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "config.toml")
file, _ := os.Open("config.toml")
io.Copy(part, file)
writer.Close()

req, _ := http.NewRequest("POST", "https://api.enconvert.com/v1/convert/toml-to-json", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-API-Key", "sk_your_private_key")
resp, _ := http.DefaultClient.Do(req)
```

---

## Risposte di errore

| Stato | Condizione |
|--------|-----------|
| `400 Bad Request` | Il file non è un file `.toml` |
| `400 Bad Request` | Contenuto TOML non valido |
| `400 Bad Request` | Conversione da TOML a JSON non riuscita |
| `401 Unauthorized` | Chiave API / token JWT mancante o non valido |
| `402 Payment Required` | Quota mensile di ops esaurita |
| `413 Payload Too Large` | Il file supera la dimensione massima prevista dal piano |

---

## Limiti

| Limite | Valore |
|-------|-------|
| Dimensione massima del file | Dipende dal piano (Founding: 5 MB) |
| Codifica di input | Solo UTF-8 |
| Conversioni mensili | Dipende dal piano |

---

## Domande frequenti

### Come convertire TOML in JSON con un'API REST?

Invia una richiesta POST `multipart/form-data` a `POST /v1/convert/toml-to-json` con il file `.toml` nel campo `file`, autenticandoti con un header `X-API-Key` o un token JWT `Authorization: Bearer`. Per impostazione predefinita il corpo della risposta contiene i byte JSON grezzi.

### Come vengono rappresentati i valori datetime TOML nell'output JSON?

I valori datetime TOML vengono serializzati come stringhe ISO-8601. Le tabelle TOML diventano oggetti JSON e gli array TOML diventano array JSON, con indentazione a 2 spazi e unicode preservato.

### Posso ottenere un URL di download prefirmato invece del JSON grezzo?

Sì. Imposta `direct_download=false` e l'API restituisce metadati JSON che includono `presigned_url`, `object_key`, `filename`, `file_size` e `conversion_time_seconds`.

### Perché la conversione da TOML a JSON restituisce un errore 400?

Un `400 Bad Request` viene restituito quando il file caricato non è un `.toml`, il contenuto TOML non è valido oppure la conversione stessa fallisce. Il file deve inoltre essere codificato in UTF-8.
