Skip to main content

Setup

using System;
using System.Text.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Legnext.SDK.Api;
using Legnext.SDK.Client;
using Legnext.SDK.Extensions;

string apiKey = Environment.GetEnvironmentVariable("LEGNEXT_API_KEY");

var services = new ServiceCollection();
services.AddApi(config =>
{
    config.AddApiHttpClients(client =>
    {
        client.BaseAddress = new Uri("https://api.legnext.ai");
    });
});

var provider = services.BuildServiceProvider();
var imageApi = provider.GetRequiredService<IImageApi>();

Text-to-Image (Diffusion)

Generate images from text descriptions:
var request = new Dictionary<string, object>
{
    { "text", "a beautiful sunset over mountains" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1DiffusionPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Job ID: {jobId.GetString()}");
    }
}
With Additional Parameters:
var request = new Dictionary<string, object>
{
    { "text", "a futuristic city at night" },
    { "aspect_ratio", "16:9" },
    { "version", "6.1" },
    { "quality", "high" },
    { "callback", "https://example.com/callback" }
};

Image Upscaling

Enhance image resolution:
var request = new Dictionary<string, object>
{
    { "job_id", "original-job-id" },
    { "imageNo", 0 },  // Index of image to upscale (0-3)
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1UpscalePostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Upscale Job ID: {jobId.GetString()}");
    }
}

Image Variation

Create variations of existing images:
var request = new Dictionary<string, object>
{
    { "job_id", "original-job-id" },
    { "imageNo", 2 },  // Which image to vary (0-3)
    { "remixPrompt", "same image but with different colors" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1VariationPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Variation Job ID: {jobId.GetString()}");
    }
}

Image Blending

Combine multiple images:
var request = new Dictionary<string, object>
{
    { "imgUrls", new List<string>
        {
            "https://example.com/image1.jpg",
            "https://example.com/image2.jpg"
        }
    },
    { "aspect_ratio", "1:1" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1BlendPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    // Process response...
}

Image Editing

Edit specific parts of an image:
var request = new Dictionary<string, object>
{
    { "canvas", new Dictionary<string, object>
        {
            { "width", 512 },
            { "height", 512 }
        }
    },
    { "imgPos", new Dictionary<string, object>
        {
            { "x", 0 },
            { "y", 0 }
        }
    },
    { "remixPrompt", "add a rainbow in the sky" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1EditPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    // Process response...
}

Image Inpainting

Inpaint specific regions of an image:
var request = new Dictionary<string, object>
{
    { "mask", new Dictionary<string, object>
        {
            { "x", 0 },
            { "y", 0 },
            { "width", 256 },
            { "height", 256 }
        }
    },
    { "remixPrompt", "a detailed painting" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1InpaintPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Inpaint Job ID: {jobId.GetString()}");
    }
}

Image Outpainting

Extend image boundaries:
var request = new Dictionary<string, object>
{
    { "scale", 1.5 },  // Scaling factor (1.1-3.0)
    { "remixPrompt", "extend the scene naturally" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1OutpaintPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Outpaint Job ID: {jobId.GetString()}");
    }
}

Image Panning

Pan across an image:
var request = new Dictionary<string, object>
{
    { "direction", 0 },  // 0=Down, 1=Right, 2=Up, 3=Left
    { "scale", 1.5 },  // Scaling factor (1.1-3.0)
    { "remixPrompt", "continue the scene" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1PanPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Pan Job ID: {jobId.GetString()}");
    }
}

Image Remixing

Remix an image with a different style or concept:
var request = new Dictionary<string, object>
{
    { "job_id", "original-job-id" },
    { "imageNo", 1 },  // Which image to remix (0-3)
    { "mode", 0 },  // 0=Strong, 1=Subtle
    { "remixPrompt", "same scene but in oil painting style" },
    { "callback", "https://example.com/callback" }
};

var response = await imageApi.ApiV1RemixPostAsync(
    new Option<string>(apiKey),
    request
);

if (response.IsOk)
{
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    if (jsonDoc.RootElement.TryGetProperty("job_id", out var jobId))
    {
        Console.WriteLine($"Remix Job ID: {jobId.GetString()}");
    }
}

Response Handling

All responses follow this pattern:
var response = await imageApi.SomeMethodAsync(...);

if (response.IsOk)
{
    // Parse the JSON response
    var jsonDoc = JsonDocument.Parse(response.RawContent);
    var root = jsonDoc.RootElement;

    // Access properties
    if (root.TryGetProperty("job_id", out var jobId))
    {
        string jobIdValue = jobId.GetString();
    }

    if (root.TryGetProperty("status", out var status))
    {
        string statusValue = status.GetString();
    }
}
else
{
    Console.WriteLine($"Request failed: {response.StatusCode}");
    Console.WriteLine($"Response: {response.RawContent}");
}

Next Steps