# Applications mobiles

Les applications iOS, Android, Flutter et React Native font appel à **votre backend**, et votre backend fait appel à TryOnCloud. N'intégrez jamais la clé API dans l'application : n'importe qui peut l'extraire du fichier d'installation de l'application.

```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
```

Créez le point de terminaison backend à l'aide de [Node.js](/docs/nodejs), [Python](/docs/python) ou [PHP](/docs/php). Appelez-le ensuite depuis l'application :

## 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()) }
```

Définissez le délai d'expiration HTTP sur au moins 120 secondes ; un essai dure environ 20 secondes.

Sur les réseaux mobiles peu fiables, faites envoyer par votre backend une `Idempotency-Key` pour chaque essai afin qu'une nouvelle tentative ne soit jamais facturée deux fois, ou utilisez une [tâche asynchrone](/docs/async) et prévenez l'application (par exemple par une notification push) à l'arrivée du webhook `generation.completed`.
