翻篇收录说明
翻篇网页快照

Image Inputs and Results: Files, URLs, and Base64 - Recraft

https://www.recraft.ai/docs/api-reference/image-inputs-and-results

最近抓取:2026-12-04 13:54:39(北京时间)

最近内容变化:2026-12-01 21:31:17(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

How to send images to the Recraft API as files, URLs, or data URLs, and how to get results as URLs, Base64, or multipart bytes.

正文摘录

How images are passed to the Recraft API and how generated images are returned. Image inputs Every endpoint that takes an image (or mask) as input accepts the request in two interchangeable formats: multipart/form-data — upload the binary file directly using form fields (image, mask, file, …). This is convenient for local files. application/json — pass the image by reference instead of uploading bytes. Each file field has a JSON counterpart that accepts a public URL or a data URL (a data:image/...;base64,... string). The mapping between the multipart file fields and their JSON counterparts: Multipart fieldJSON fieldTypeEndpointsimageimage_urlstring — URL or data URL of the input imageImage to imageImage inpaintingImage outpaintingReplace backgroundGenerate backgroundErase regionRemix imagemaskmask_urlstring — URL or data URL of the mask imageImage inpaintingGenerate backgroundErase regionfileimage_urlstring — URL or data URL of the input imageVectorize imageRemove backgroundCrisp upscaleCreative upscaleEnhance detailsfilesimage_urlsarray of strings — URLs or data URLs of the input imagesCreate stylestyle_referencesstyle_reference_urlsarray of strings — URLs or data URLs of the style reference imagesGenerate image All other parameters are identical between the two formats. Use JSON when your image is already hosted somewhere (just send the URL) or when a JSON body fits your client better; use multipart to upload local files directly. Examples Generate an image with a style reference passed as a file and as a URL: multipart/form-data PythonPython (OpenAI SDK)cURLimport os import requests with open('reference.png', 'rb') as reference: response = requests.post( 'https://external.api.recraft.ai/v1/images/generations', headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"}, data={'prompt': 'product hero shot, bottle on a table'}, files={'style_references': reference}, ) if not response.ok: raise SystemExit(f'{response.status_code}: {response.text}') print(response.json()['data'][0]['url']) application/json PythonPython (OpenAI SDK)cURLimport os import requests response = requests.post( 'https://external.api.recraft.ai/v1/images/generations', headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"}, json={ 'prompt': 'product hero shot, bottle on a table', 'style_reference_urls': ['https://example.com/reference.png'], }, ) if not response.ok: raise SystemExit(f'{response.status_code}: {response.text}') print(response.json()['data'][0]['url']) Image results Every endpoint that returns images takes the response_format parameter: url (default) — a JSON response, every image carries a url to download it from. b64_json — a JSON response, every image carries its bytes Base64-encoded in b64_json. multipart — a multipart/form-data response. The response part carries the same JSON with an image_id per image, followed by one part per image with the raw bytes. The part is named by the image_id, which is also in its Content-ID header, and its Content-Type is the image media type (image/png, image/webp or image/svg+xml). For the best latency use response_format: multipart. With zero data retention, an image returned as b64_json or multipart is not stored; an image returned as url is uploaded so that the URL can be served. Errors keep their non-2xx status with every response_format. The error body is plain text or JSON, not multipart; see Errors. The OpenAI Python library only reads JSON responses, so use url or b64_json with it. Examples url PythonPython (OpenAI SDK)cURLimport os import requests response = requests.post( 'https://external.api.recraft.ai/v1/images/generations', headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"}, json={'prompt': 'race car on a track'}, ) if not response.ok: raise SystemExit(f'{response.status_code}: {response.text}') print(response.json()['data'][0]['url']) b64_json PythonPython (OpenAI SDK)cURLimport base64 import os import requests response = requests.post( 'https://external.api.recraft.ai/v1/images/generations', headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"}, json={'prompt': 'race car on a track', 'response_format': 'b64_json'}, ) if not response.ok: raise SystemExit(f'{response.status_code}: {response.text}') with open('image.webp', 'wb') as f: f.write(base64.b64decode(response.json()['data'][0]['b64_json'])) multipart PythoncURL# pip install requests requests-toolbelt import json import os import requests from requests_toolbelt.multipart import decoder response = requests.post( 'https://external.api.recraft.ai/v1/images/generations', headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"}, json={'prompt': 'race car on a track', 'response_format': 'multipart'}, ) if not response.ok: raise SystemExit(f'{response.status_code}: {response.text}') result, images = None, {} for part in decoder.MultipartDecoder.from_response(response).parts: image_id = part.headers.get(b'Content-ID') if image_id is None: result = json.loads(part.content) else: images[image_id.decode().strip('<>')] = part.content for image in result['data']: with open(f"{image['image_id']}.webp", 'wb') as f: f.write(images[image['image_id']]) Image format Every endpoint that returns raster images also takes the image_format parameter, which sets the file format of the result: webp (default) — lossless WebP. png — PNG. Vector images are always returned as SVG, image_format does not affect them. With response_format: multipart the Content-Type of every image part follows the chosen format.