# App mobili

Le app iOS, Android, Flutter e React Native si collegano **al tuo backend**, e il tuo backend si collega a TryOnCloud. Non inserire mai la chiave API nell'app: chiunque può estrarla dal pacchetto dell'app.

```flow
Mobile app -> Your backend: POST /try-on (photo + product image URL)
Your backend -> TryOnCloud API: POST /api/v1/generate (X-API-KEY)
TryOnCloud API --> Your backend: 200 image/png
Your backend --> Mobile app: 200 image/png
```

Crea l'endpoint del backend utilizzando [Node.js](/docs/nodejs), [Python](/docs/python) o [PHP](/docs/php). Quindi richiamalo dall'app:

## React Native

```js
const form = new FormData()
form.append("person", { uri: photo.uri, name: "person.jpg", type: "image/jpeg" })
form.append("garmentUrl", product.imageUrl)

const res = await fetch("https://api.yourapp.com/try-on", { method: "POST", body: form })
// Show the result: save the PNG with expo-file-system or react-native-fs, then render it.
```

## Flutter

```dart
final req = http.MultipartRequest('POST', Uri.parse('https://api.yourapp.com/try-on'))
  ..files.add(await http.MultipartFile.fromPath('person', photo.path))
  ..fields['garmentUrl'] = product.imageUrl;
final res = await http.Response.fromStream(await req.send());
if (res.statusCode == 200) {
  setState(() => resultBytes = res.bodyBytes); // Image.memory(resultBytes!)
}
```

## Swift (iOS)

```swift
var req = URLRequest(url: URL(string: "https://api.yourapp.com/try-on")!)
req.httpMethod = "POST"
req.timeoutInterval = 120
// Build a multipart body with "person" (JPEG data) and "garmentUrl", then:
let (data, response) = try await URLSession.shared.upload(for: req, from: body)
if (response as? HTTPURLResponse)?.statusCode == 200 { image = UIImage(data: data) }
```

## Kotlin (Android)

```kotlin
val body = MultipartBody.Builder().setType(MultipartBody.FORM)
    .addFormDataPart("person", "person.jpg", photoFile.asRequestBody("image/jpeg".toMediaType()))
    .addFormDataPart("garmentUrl", product.imageUrl)
    .build()
val request = Request.Builder().url("https://api.yourapp.com/try-on").post(body).build()
client.newCall(request).execute().use { res -> if (res.isSuccessful) bitmap = BitmapFactory.decodeStream(res.body!!.byteStream()) }
```

Imposta il timeout HTTP su almeno 120 secondi; una prova di vestibilità richiede circa 20 secondi.

Sulle reti mobili poco affidabili, fai inviare al tuo backend una `Idempotency-Key` per ogni prova, così un nuovo tentativo non viene mai addebitato due volte, oppure usa un'[operazione asincrona](/docs/async) e avvisa l'app (ad esempio con una notifica push) quando arriva il webhook `generation.completed`.
