from magic_hour import Client
from os import getenv
client = Client(token=getenv("MAGIC_HOUR_API_KEY"))
res = client.v1.body_swap.generate(
assets={
"person_file_path": "/path/to/1234.png",
"scene_file_path": "/path/to/5678.png",
},
resolution="640px",
name="My Body Swap image",
wait_for_completion=True,
download_outputs=True,
download_directory="."
)import { Client } from "magic-hour";
const client = new Client({ token: process.env["MAGIC_HOUR_API_KEY"] });
const res = await client.v1.bodySwap.generate(
{
assets: {
personFilePath: "/path/to/person.png",
sceneFilePath: "/path/to/scene.png",
},
name: "My Body Swap image",
resolution: "640px",
},
{
waitForCompletion: true,
downloadOutputs: true,
downloadDirectory: ".",
},
);package main
import (
os "os"
sdk "github.com/magichourhq/magic-hour-go/client"
nullable "github.com/magichourhq/magic-hour-go/nullable"
body_swap "github.com/magichourhq/magic-hour-go/resources/v1/body_swap"
types "github.com/magichourhq/magic-hour-go/types"
)
func main() {
client := sdk.NewClient(
sdk.WithBearerAuth(os.Getenv("MAGIC_HOUR_API_KEY")),
)
res, err := client.V1.BodySwap.Create(body_swap.CreateRequest{
Assets: types.V1BodySwapCreateBodyAssets{
PersonFilePath: "api-assets/id/1234.png",
SceneFilePath: "api-assets/id/5678.png",
},
Name: nullable.NewValue("My Body Swap image"),
Resolution: types.V1BodySwapCreateBodyResolutionEnum640px,
})
}let client = magic_hour::Client::default()
.with_bearer_auth(&std::env::var("MAGIC_HOUR_API_KEY").unwrap());
let res = client
.v1()
.body_swap()
.create(magic_hour::resources::v1::body_swap::CreateRequest {
assets: magic_hour::models::V1BodySwapCreateBodyAssets {
person_file_path: "api-assets/id/1234.png".to_string(),
scene_file_path: "api-assets/id/5678.png".to_string(),
},
name: Some("My Body Swap image".to_string()),
resolution: magic_hour::models::V1BodySwapCreateBodyResolutionEnum::Enum640px,
})
.await;curl --request POST \
--url https://api.magichour.ai/v1/body-swap \
--header 'accept: application/json' \
--header "authorization: Bearer $MAGIC_HOUR_API_KEY" \
--header 'content-type: application/json' \
--data '
{
"name": "My Body Swap image",
"resolution": "640px",
"assets": {
"person_file_path": "api-assets/id/1234.png",
"scene_file_path": "api-assets/id/5678.png"
}
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.magichour.ai/v1/body-swap",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'My Body Swap image',
'resolution' => '640px',
'assets' => [
'person_file_path' => 'api-assets/id/1234.png',
'scene_file_path' => 'api-assets/id/5678.png'
]
]),
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authorization: Bearer <token>",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.magichour.ai/v1/body-swap")
.header("accept", "application/json")
.header("content-type", "application/json")
.header("authorization", "Bearer <token>")
.body("{\"name\":\"My Body Swap image\",\"resolution\":\"640px\",\"assets\":{\"person_file_path\":\"api-assets/id/1234.png\",\"scene_file_path\":\"api-assets/id/5678.png\"}}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.magichour.ai/v1/body-swap")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"resolution\": \"640px\",\n \"assets\": {\n \"person_file_path\": \"api-assets/id/1234.png\",\n \"scene_file_path\": \"api-assets/id/5678.png\"\n },\n \"name\": \"My Body Swap image\"\n}"
response = http.request(request)
puts response.read_body{
"id": "cuid-example",
"credits_charged": 50
}{
"code": "invalid_request",
"message": "<string>"
}{
"code": "unauthorized",
"message": "<string>"
}{
"code": "insufficient_credits",
"message": "<string>",
"credit_shortfall": 123,
"credits_available": 123,
"credits_required": 123,
"next_action": {
"type": "check_balance",
"reason": "auto_reload_pending"
}
}{
"code": "not_found",
"message": "<string>"
}{
"code": "unprocessable_entity",
"message": "<string>"
}{
"code": "internal_server_error",
"message": "<string>"
}Body Swap
Swap a person into a scene image using Nano Banana 2 Lite (640px/1k) or Nano Banana 2.1 (2k/4k). Credits depend on resolution (from 50 credits at 640px upward).
from magic_hour import Client
from os import getenv
client = Client(token=getenv("MAGIC_HOUR_API_KEY"))
res = client.v1.body_swap.generate(
assets={
"person_file_path": "/path/to/1234.png",
"scene_file_path": "/path/to/5678.png",
},
resolution="640px",
name="My Body Swap image",
wait_for_completion=True,
download_outputs=True,
download_directory="."
)import { Client } from "magic-hour";
const client = new Client({ token: process.env["MAGIC_HOUR_API_KEY"] });
const res = await client.v1.bodySwap.generate(
{
assets: {
personFilePath: "/path/to/person.png",
sceneFilePath: "/path/to/scene.png",
},
name: "My Body Swap image",
resolution: "640px",
},
{
waitForCompletion: true,
downloadOutputs: true,
downloadDirectory: ".",
},
);package main
import (
os "os"
sdk "github.com/magichourhq/magic-hour-go/client"
nullable "github.com/magichourhq/magic-hour-go/nullable"
body_swap "github.com/magichourhq/magic-hour-go/resources/v1/body_swap"
types "github.com/magichourhq/magic-hour-go/types"
)
func main() {
client := sdk.NewClient(
sdk.WithBearerAuth(os.Getenv("MAGIC_HOUR_API_KEY")),
)
res, err := client.V1.BodySwap.Create(body_swap.CreateRequest{
Assets: types.V1BodySwapCreateBodyAssets{
PersonFilePath: "api-assets/id/1234.png",
SceneFilePath: "api-assets/id/5678.png",
},
Name: nullable.NewValue("My Body Swap image"),
Resolution: types.V1BodySwapCreateBodyResolutionEnum640px,
})
}let client = magic_hour::Client::default()
.with_bearer_auth(&std::env::var("MAGIC_HOUR_API_KEY").unwrap());
let res = client
.v1()
.body_swap()
.create(magic_hour::resources::v1::body_swap::CreateRequest {
assets: magic_hour::models::V1BodySwapCreateBodyAssets {
person_file_path: "api-assets/id/1234.png".to_string(),
scene_file_path: "api-assets/id/5678.png".to_string(),
},
name: Some("My Body Swap image".to_string()),
resolution: magic_hour::models::V1BodySwapCreateBodyResolutionEnum::Enum640px,
})
.await;curl --request POST \
--url https://api.magichour.ai/v1/body-swap \
--header 'accept: application/json' \
--header "authorization: Bearer $MAGIC_HOUR_API_KEY" \
--header 'content-type: application/json' \
--data '
{
"name": "My Body Swap image",
"resolution": "640px",
"assets": {
"person_file_path": "api-assets/id/1234.png",
"scene_file_path": "api-assets/id/5678.png"
}
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.magichour.ai/v1/body-swap",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'My Body Swap image',
'resolution' => '640px',
'assets' => [
'person_file_path' => 'api-assets/id/1234.png',
'scene_file_path' => 'api-assets/id/5678.png'
]
]),
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authorization: Bearer <token>",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.magichour.ai/v1/body-swap")
.header("accept", "application/json")
.header("content-type", "application/json")
.header("authorization", "Bearer <token>")
.body("{\"name\":\"My Body Swap image\",\"resolution\":\"640px\",\"assets\":{\"person_file_path\":\"api-assets/id/1234.png\",\"scene_file_path\":\"api-assets/id/5678.png\"}}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.magichour.ai/v1/body-swap")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"resolution\": \"640px\",\n \"assets\": {\n \"person_file_path\": \"api-assets/id/1234.png\",\n \"scene_file_path\": \"api-assets/id/5678.png\"\n },\n \"name\": \"My Body Swap image\"\n}"
response = http.request(request)
puts response.read_body{
"id": "cuid-example",
"credits_charged": 50
}{
"code": "invalid_request",
"message": "<string>"
}{
"code": "unauthorized",
"message": "<string>"
}{
"code": "insufficient_credits",
"message": "<string>",
"credit_shortfall": 123,
"credits_available": 123,
"credits_required": 123,
"next_action": {
"type": "check_balance",
"reason": "auto_reload_pending"
}
}{
"code": "not_found",
"message": "<string>"
}{
"code": "unprocessable_entity",
"message": "<string>"
}{
"code": "internal_server_error",
"message": "<string>"
}Authorizations
Bearer authentication header of the form Bearer <api_key>, where <api_key> is your API key. To get your API key, go to Developer Hub and click "Create new API Key".
Headers
A unique string for this request, such as a UUID. Sending the same key again within 24 hours returns the original project instead of creating and charging for a new one. A key applies across all video, image, and audio endpoints on your account, so use a new key for every project.
"5f3c1b9e-8a41-4a0e-9d3e-2f6a1c7b9e10"
Body
Body
Response
Success
Success
Unique ID of the image. Use it with the Get image Project API to fetch status and downloads.
"cuid-example"
Was this page helpful?