Deploy an image generation endpoint from the Hub and integrate it into a web application.
In this tutorial, you’ll deploy a pre-built SDXL Turbo from the and integrate it into a web application. You’ll build a simple frontend that sends prompts to your endpoint and displays the generated images.By the end, you’ll know how to deploy endpoints from the Hub and integrate them into your applications using standard HTTP requests.
The Runpod Hub provides pre-built workers you can deploy with a few clicks. For this tutorial, you’ll deploy the SDXL Turbo worker, which generates images from text prompts.
Test your endpoint with a simple API request before integrating it into an application. The endpoint accepts a JSON payload with an input object containing your prompt.
cURL
Python
Response
Copy
curl -X POST "https://api.runpod.ai/v2/${YOUR_ENDPOINT_ID}/runsync" \ -H "accept: application/json" \ -H "content-type: application/json" \ -H "authorization: Bearer ${YOUR_API_KEY}" \ -d '{ "input": { "prompt": "A futuristic city skyline at sunset" } }'
Copy
import requestsendpoint_id = "YOUR_ENDPOINT_ID"api_key = "YOUR_API_KEY"response = requests.post( f"https://api.runpod.ai/v2/{endpoint_id}/runsync", headers={ "accept": "application/json", "content-type": "application/json", "authorization": f"Bearer {api_key}", }, json={ "input": { "prompt": "A futuristic city skyline at sunset", } },)print(response.json())
The generated image is returned as a base64-encoded PNG string in output.images[0].image. For more information about request parameters and response formats, see Send requests.SDXL Turbo uses adversarial diffusion distillation to generate images in just a few steps. The worker is pre-configured with optimal settings, so you only need to provide the prompt parameter, but you can also provide other parameters to control the generation process.
Now that you’ve confirmed your endpoint is working, you can integrate it into a web application. This example creates a simple image generator with an input field for prompts and a button to generate images. While this tutorial uses JavaScript, you can make requests to your endpoint using any programming language or framework.Create two files in the same directory:
index.html
script.js
The HTML file sets up a user interface with an input field for prompts and a button to generate images. Clicking the button calls the generateImage function in script.js.
The JavaScript file handles API communication. The generateImage function reads the user’s prompt, sends a POST request to your Runpod endpoint, and displays the generated image. The endpoint returns a base64-encoded PNG image in the output.images array, which is then converted to a displayable format.
script.js
Copy
// Replace these with your actual valuesconst ENDPOINT_ID = "YOUR_ENDPOINT_ID";const API_KEY = "YOUR_API_KEY";async function generateImage() { const prompt = document.getElementById("promptInput").value; if (!prompt) { alert("Please enter a prompt!"); return; } // Show loading indicator document.getElementById("loading").style.display = "block"; document.getElementById("imageResult").innerHTML = ""; const options = { method: "POST", headers: { "accept": "application/json", "content-type": "application/json", "authorization": `Bearer ${API_KEY}`, }, body: JSON.stringify({ input: { prompt: prompt, }, }), }; try { const response = await fetch( `https://api.runpod.ai/v2/${ENDPOINT_ID}/runsync`, options ); const data = await response.json(); // Hide loading indicator document.getElementById("loading").style.display = "none"; if (data && data.output && data.output.images && data.output.images.length > 0) { const imageBase64 = data.output.images[0].image; const imageUrl = `data:image/png;base64,${imageBase64}`; document.getElementById("imageResult").innerHTML = `<img src="${imageUrl}" alt="Generated Image" />`; } else if (data && data.error) { alert(`Error: ${data.error}`); } else { alert("Failed to generate image. Check the console for details."); console.error("Response:", data); } } catch (error) { document.getElementById("loading").style.display = "none"; console.error("Error:", error); alert("Error generating image. Check the console for details."); }}
Before running the application:
Replace YOUR_ENDPOINT_ID with your endpoint ID from step 2.
Replace YOUR_API_KEY with your Runpod API key.
This example includes the API key directly in client-side code for simplicity. In production, you should make API calls from a backend server to keep your API key secure.
You can run the application using a local server or by opening the HTML file directly in a browser.
Python server
Open directly
Run the following command in the directory containing your files:
Copy
python -m http.server 8000
Then open http://localhost:8000 in your browser.
Navigate to the folder containing index.html.
Double-click the file to open it in your default browser, or right-click and select Open with to choose a specific browser.
Enter a prompt and click Generate Image to see your AI-generated image.
The first request may take longer (30-60 seconds) due to as the endpoint loads the model into GPU memory. Subsequent requests complete in just a few seconds.