Gunakan Postman atau cURL untuk memanggil API pembuatan gambar dan video
Artikel ini menjelaskan cara menggunakan Postman dan cURL untuk memanggil API pembuatan gambar atau video di Alibaba Cloud Model Studio (Bailian). Panduan ini menggunakan contoh text-to-image untuk menunjukkan proses lengkap, mulai dari membuat task hingga mengambil hasilnya.
Postman: Alat grafis intuitif untuk menguji permintaan HTTP. Direkomendasikan untuk pemula.
cURL: Alat command-line yang andal untuk developer yang terbiasa menggunakan command line.
Postman dan cURL ditujukan untuk pengujian cepat dan verifikasi fungsional. Untuk lingkungan produksi, gunakan SDK resmi atau implementasikan panggilan HTTP Anda sendiri.
Tugas pembuatan gambar dan video dapat memakan waktu lama hingga selesai, mulai dari lebih dari sepuluh detik hingga beberapa menit. Untuk menghindari koneksi HTTP yang berjalan lama dan timeout, API menggunakan mekanisme panggilan asinkron. Proses ini dibagi menjadi dua langkah:
Buat task: Panggil API untuk membuat task. Layanan akan secara sinkron mengembalikan ID task (task_id).
Kueri hasil: Gunakan ID task untuk melakukan polling status task hingga task selesai, lalu ambil URL gambar atau video akhir.
Gunakan perintah cURL berikut untuk mengonfigurasi Postman.
URL dasar berikut berlaku untuk wilayah Singapura. Anda harus menggunakan URL dasar yang sesuai untuk wilayah Anda.
Copy
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis \ -H 'X-DashScope-Async: enable' \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "model": "wan2.5-t2i-preview", "input": { "prompt": "A flower shop with exquisite windows, a beautiful wooden door, and flowers on display" }, "parameters": { "size": "1024*1024", "n": 1 }}'
Di Postman, klik tombol new atau + untuk membuat permintaan baru, lalu pilih HTTP sebagai jenis permintaan.
Dari drop-down metode permintaan, pilih POST dan masukkan URL untuk wilayah penerapan model Anda:
Singapura: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis, dan ganti WorkspaceId dengan ID Ruang Kerja Anda yang sebenarnya seperti dijelaskan dalam Dapatkan ID Ruang Kerja.
AS (Virginia): https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
China (Beijing): https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
Untuk model yang didukung di setiap wilayah, lihat Konsol Model Studio. Wilayah saat ini dan cakupan penerapan layanan didasarkan pada hubungan binding sistem yang telah ditentukan, dan kombinasi kustom tidak didukung.
Copy
POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
Klik tab Headers dan tambahkan tiga pasangan kunci-nilai berikut.
Key
Value
Deskripsi
Di Postman, pada tab Headers, atur X-DashScope-Async ke enable, Authorization ke Bearer <your-api-key>, dan Content-Type ke application/json.
X-DashScope-Async
enable
Mengaktifkan panggilan asinkron.
Authorization
Bearer sk-xxx (Ganti sk-xxx dengan Kunci API Anda)
Kredensial autentikasi.
Content-Type
application/json
Menyatakan bahwa badan permintaan berformat JSON.
Konfigurasi badan permintaan:
Klik tab Body, pilih tombol radio raw, pilih JSON dari menu drop-down format di sebelah kanan, lalu tempel konten JSON yang mengikuti -d dari contoh cURL ke dalam kotak input.
Copy
{ "model": "wan2.5-t2i-preview", "input": { "prompt": "A flower shop with exquisite windows, a beautiful wooden door, and flowers on display" }, "parameters": { "size": "1024*1024", "n": 1 }}
(Opsional) Klik Beautify di sisi kanan halaman untuk memformat JSON agar lebih mudah dibaca.
Klik Send untuk mengirim permintaan dan mendapatkan task_id. task_id berlaku selama 24 jam. Setelah periode ini, hasil tidak dapat dikueri. Harap segera ambil hasilnya.
Setelah Anda memiliki ID task, panggil endpoint kueri untuk mengambil hasil akhir.
Singapura: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis, dan ganti WorkspaceId dengan ID Ruang Kerja Anda yang sebenarnya.
AS (Virginia): https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
China (Beijing): https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
Copy
curl -X GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/tasks/{task_id} \--header "Authorization: Bearer $DASHSCOPE_API_KEY"
Konfigurasi permintaan kueri di Postman:
Buat permintaan HTTP baru.
Atur metode permintaan ke GET.
Berdasarkan wilayah, masukkan URL kueri, dan ganti {task_id} di URL dengan task_id yang sebenarnya yang Anda peroleh di Langkah 1.
Di tab Headers, tambahkan header Authorization, menggunakan Kunci API yang sama seperti di Langkah 1.
Klik Send.
Copy
GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/42442de9-917d-4c41-80a7-37fb7ad25ed2Headers:Cookie: acw_tc=896b2ccd-a0cd-40a8-a557-bb73cee5cf957...Postman-Token: <calculated when request is sent>Host: <calculated when request is sent>User-Agent: PostmanRuntime/7.49.1Accept: */*Accept-Encoding: gzip, deflate, brConnection: keep-aliveAuthorization: Bearer sk-xxx
Periksa respons. Lakukan polling endpoint ini hingga task_status bernilai SUCCEEDED untuk mendapatkan URL gambar. URL gambar berlaku selama 24 jam. Pastikan Anda mengunduh gambar tersebut dalam periode validitas 24 jam.
Copy
"results": [ { "orig_prompt": "A flower shop with exquisite windows, a beautiful wooden door, and flowers on display", "actual_prompt": "A cozy and elegant flower shop, with an exterior decorated with exquisitely carved windows. The window frames are light-colored wood, set with transparent glass that reveals the soft light inside. The front door is a beautiful solid wood door that retains its natural grain, complemented by a brass handle and a vintage nameplate, giving it a quaint and textured feel. On both sides of the front steps, and on the windowsills and entrance area, various flowers—including roses, lilies, hydrangeas, and tulips—are arranged in a staggered, vibrant display. Some bouquets are wrapped in kraft paper and tied with twine, exuding a fresh, natural scent. The background features a quiet European town streetscape, with stone-paved roads and greenery adding to the charm. The overall image is in a realistic photographic style, with soft lighting and a warm color palette that highlights the shop's exquisite and romantic atmosphere.", "url": "https://dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com/1d/3a/20251203/7a2bee47/42442de9-917d-4c41-80a7-37fb7ad25ed2.png?xxx" } ]
URL dasar berikut berlaku untuk wilayah Singapura. Anda harus menggunakan URL dasar yang sesuai untuk wilayah Anda.
Copy
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis \ -H 'X-DashScope-Async: enable' \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "model": "wan2.5-t2i-preview", "input": { "prompt": "A flower shop with exquisite windows, a beautiful wooden door, and flowers on display" }, "parameters": { "size": "1024*1024", "n": 1 }}'
Permintaan yang berhasil akan mengembalikan task_id. ID ini berlaku selama 24 jam dan tidak dapat digunakan untuk kueri setelah kedaluwarsa. Harap segera ambil hasilnya.
Copy
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis \ -H 'X-DashScope-Async: enable' \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "model": "wan2.5-t2i-preview", "input": { "prompt": "A flower shop with exquisite windows, a beautiful wooden door, and flowers on display" }, "parameters": { "size": "1024*1024", "n": 1 }}'{"request_id":"ca3b5224-bc60-4e47-80f4-2790040e9e55","output":{"task_id":"2de3f7e1-52ce-4cf4-a466-f2d318061c43","task_status":"PENDING"}}
Ganti {task_id} pada perintah berikut dengan ID task yang Anda peroleh di Langkah 1, salin perintah tersebut ke terminal, lalu jalankan.
URL dasar berikut berlaku untuk wilayah Singapura. Anda harus menggunakan URL dasar yang sesuai untuk wilayah Anda.
Copy
curl -X GET https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/tasks/{task_id} \--header "Authorization: Bearer $DASHSCOPE_API_KEY"
Saat task selesai (nilai task_status adalah SUCCEEDED), respons akan berisi URL gambar. URL gambar berlaku selama 24 jam. Unduh segera.
Karena model membutuhkan waktu lama untuk memproses (puluhan detik hingga beberapa menit), Anda mungkin perlu melakukan polling API ini. Kami menyarankan Anda mengkueri API setiap 3 hingga 5 detik hingga nilai task_status tidak lagi RUNNING.
Copy
curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/2de3f7e1-52ce-4cf4-a466-f2d3f8061c43 \--header "Authorization: Bearer $DASHSCOPE_API_KEY"{"request_id":"24143559-9964-4898-8f9c-d968bba182b9","output":{"task_id":"2de3f7e1-52ce-4cf4-a466-f2d3f8061c43","task_status":"SUCCEEDED","submit_time":"2025-12-03 10:19:18.293","scheduled_time":"2025-12-03 10:19:18.320","end_time":"2025-12-03 10:19:31.890","results":[{"orig_prompt":"A flower shop with exquisite windows, a beautiful wooden door, and flowers on display","actual_prompt":"A cozy, elegant flower shop. The exterior wall features exquisitely carved windows with dark brown wooden frames, and the glass is clean and bright, revealing the soft light inside. The front door is a beautiful solid wood door that retains its natural grain, complemented by a brass handle and a vintage nameplate that exudes a strong artisanal feel. On both sides of the front steps, and on the windowsills and entrance area, various flowers—including blooming roses, elegant hydrangeas, fresh daisies, and green eucalyptus leaves—are arranged in a staggered yet harmonious display. The background is a quiet European town streetscape, with a stone-paved road and a vine-covered neighboring wall enhancing the romantic atmosphere. The overall image is in a realistic photographic style, with soft, natural lighting that highlights the wood texture and floral details.","url":"https://dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com/1d/59/20251203/7a2bee47/2de3f7e1-52ce-4cf4-a466-f2d3f8061c43.png?Expires=1764814771&OSSAccessKeyId=YOUR_ACCESS_KEY_ID"}],"task_metrics":{"TOTAL":1,"FAILED":0,"SUCCEEDED":1}},"usage":{"image_count":1}}
Copy
curl -X GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/8b108773-4e7d-4cd7-8c62-aad7a08d7769 \--header "Authorization: Bearer $DASHSCOPE_API_KEY"{"request_id":"0ee9ab8a-0ea4-9f0b-9cb0-41eaf24fd5e8","output":{"task_id":"8b108773-4e7d-4cd7-8c62-aad7a08d7769","task_status":"SUCCEEDED","submit_time":"2025-12-03 10:50:24.253","scheduled_time":"2025-12-03 10:50:24.282","end_time":"2025-12-03 10:50:42.708","results":[{"orig_prompt":"A flower shop with exquisite windows, a beautiful wooden door, and flowers on display","actual_prompt":"A warm and elegant flower shop, with an exterior wall decorated with an exquisitely carved window. The window frame is dark brown solid wood, with clean and bright glass revealing the soft light inside. The front door is a beautiful wooden door crafted with a vintage distressed technique, its surface retaining the wood grain texture, complemented by a brass door handle and a small nameplate decoration, exuding a strong handmade feel. In front of the door, on the windowsill, and on both sides of the entrance, various flowers are arranged in a staggered fashion—pink and white roses, pale purple hydrangeas, bright yellow sunflowers, and green eucalyptus leaves—with soft and layered colors. The background is a quiet European-style street, with cobblestones and vine-covered neighboring walls adding a romantic atmosphere. The overall image is in a realistic photography style, with warm and natural lighting, highlighting the tranquility and beauty of the flower shop.","url":"https://dashscope-result-sgp.oss-ap-southeast-1.aliyuncs.com/1d/63/20251203/c3a8f0e2/24817593-8b108773-4e7d-4cd7-8c62-aad7a08d7769.png?Expires=1748416642&OSSAccessKeyId=YOUR_ACCESS_KEY_ID"}],"task_metrics":{"TOTAL":1,"FAILED":0,"SUCCEEDED":1}},"usage":{"image_count":1}}%
Jelajahi lebih banyak model: Buka Konsol Model Studio untuk melihat semua model gambar dan video yang didukung oleh Alibaba Cloud Model Studio (Bailian).