lowpoly 將於 2026年10月30日 下線。 meshy-7 作為 3D 生成模型將於 2026年12月31日 下線;作為貼圖模型仍可使用。 請在這些日期前切換模型,以免請求出錯。 查看所有棄用資訊

快速開始

先建立你的 API key

本指南中的每個請求都需要用到它。建立一個 key 只需幾秒鐘。

建立你的 API key

透過四個步驟,使用 Meshy API v2 製作你的第一個 3D 模型。你需要取得一個 API key、建立一個任務、在 Tasks 路由上追蹤它,然後下載結果。


1取得您的 API 金鑰

在開發者平台的 API Keys 頁面上建立一組 API 金鑰。離開該頁面後您將無法再次檢視該金鑰,因此請妥善保存。請在每次請求中以 Authorization 標頭中的 Bearer 權杖形式傳送該金鑰。請參閱Authentication。

您需要付費方案才能建立與管理 API 金鑰。在免費方案中,您無法建立自己的金鑰,Playground 會使用內建金鑰進行測試。

在開發者平台中開啟 API Keys 頁面,然後建立您的金鑰

請將金鑰儲存在環境變數中,而不是直接貼到程式碼裡。請在終端機中執行以下指令:

Export your key

export MESHY_API_KEY="<your-api-key>"

JavaScript 範例使用 fetch,該功能內建於 Node.js 18 及更新版本中。Python 範例使用 requests(pip install requests)。


2建立一個 Image to 3D 任務

將一張圖片傳送到 Image to 3D。如果想改為從文字提示或多張照片開始,可以使用 Text to 3D 或 Multi-Image to 3D。從這裡開始,每個 endpoint 的用法都是一樣的。

以下範例均使用這張照片,並使用 texture_model 所選擇的 Meshy 7 為模型產生 texture。如需嘗試其他圖片,請修改 image_url。如需選擇其他 texture 模型,請參閱 Texture models。

Sample fantasy character illustration used as the Image to 3D input in this guide
Sample input

Request

POST
/openapi/v2/image-to-3d
curl https://api.meshy.ai/openapi/v2/image-to-3d \
  -X POST \
  -H "Authorization: Bearer ${MESHY_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "image_url": "https://docs.meshy.ai/images/api/quick-start/source-photo.webp",
    "texture_model": "meshy-7"
  }'

回應為 202 Accepted,並回傳 task 物件。請儲存其中的 id,因為接下來的每一步都需要用到它。Location 回應標頭給出了讀取該任務的路徑。

Response: 202 Accepted

{
  "id": "018a210d-8ba4-705c-b111-1f1776f7f578",
  "type": "image-to-3d",
  "status": "PENDING",
  "progress": 0,
  "created_at": 1791846275575,
  "started_at": null,
  "finished_at": null,
  "expires_at": null,
  "preceding_tasks": 1,
  "consumed_credits": 30,
  "task_error": null,
  "input": {
    "image_url": "https://docs.meshy.ai/images/api/quick-start/source-photo.webp",
    "texture_model": "meshy-7"
  },
  "output": null
}

3取得任務直到完成

任務在背景執行,通常需要大約 2 分鐘。從 /openapi/v2/tasks/<task_id> 取得任務資訊,直到 status 變為 SUCCEEDED、FAILED 或 CANCELED。在兩次請求之間,請等待 Retry-After 回應標頭中指定的秒數。

同一路由適用於來自所有 endpoint 的任務。有關典型處理時間,以及放棄等待之前應等多久,請參閱處理時間和應等待多久。

Request

GET
/openapi/v2/tasks/:id
curl https://api.meshy.ai/openapi/v2/tasks/<task_id> \
  -H "Authorization: Bearer ${MESHY_API_KEY}"

Response: SUCCEEDED

{
  "id": "018a210d-8ba4-705c-b111-1f1776f7f578",
  "type": "image-to-3d",
  "status": "SUCCEEDED",
  "progress": 100,
  "created_at": 1791846275575,
  "started_at": 1791846275578,
  "finished_at": 1791846334536,
  "expires_at": 1792105534536,
  "preceding_tasks": 0,
  "consumed_credits": 30,
  "task_error": null,
  "input": {
    "image_url": "https://docs.meshy.ai/images/api/quick-start/source-photo.webp",
    "texture_model": "meshy-7"
  },
  "output": {
    "model_urls": {
      "glb": "https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/model.glb?Expires=***"
    },
    "thumbnail_url": "https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/preview.png?Expires=***",
    "thumbnail_urls": null,
    "texture_urls": [
      {
        "base_color": "https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/texture_0.png?Expires=***",
        "metallic": null,
        "roughness": null,
        "normal": null,
        "metallic_roughness": null,
        "emission": null
      }
    ]
  }
}

如果任務失敗,status 會變為 FAILED,task_error 會說明原因。請修正輸入內容或重新建立一個任務。請參閱 錯誤。


4下載你的模型

檔案位於 output 中。output.model_urls.glb 是一個簽名 URL,在 expires_at 之前都有效。你也可以將其貼到瀏覽器中下載檔案。

影象生成 3D 預設只回傳 GLB 檔案,除非你在建立任務時在 target_formats 中列出其他格式。

Download

output.model_urls.glb
curl -L "<output.model_urls.glb from the response above>" -o model.glb
The resulting 3D model generated from the sample fantasy character photo
Your result

整合所有程式碼

這段腳本會建立任務、等待任務完成,然後下載模型。

Full script

import { writeFile } from "node:fs/promises";

const API = "https://api.meshy.ai/openapi/v2";
const headers = {
  Authorization: `Bearer ${process.env.MESHY_API_KEY}`,
  "Content-Type": "application/json",
};

async function call(path, init = {}) {
  const res = await fetch(`${API}${path}`, {
    ...init,
    headers,
    signal: AbortSignal.timeout(init.method === "POST" ? 60_000 : 30_000),
  });
  if (!res.ok) throw new Error(`${res.status}: ${(await res.json()).message}`);
  return { body: await res.json(), retryAfter: Number(res.headers.get("retry-after")) || 5 };
}

// 1. Create the task
let { body: task, retryAfter } = await call("/image-to-3d", {
  method: "POST",
  body: JSON.stringify({
    image_url: "https://docs.meshy.ai/images/api/quick-start/source-photo.webp",
    texture_model: "meshy-7",
  }),
});

// 2. Get the task until it finishes, for up to 60 minutes
const deadline = task.created_at + 60 * 60 * 1000;
while (!["SUCCEEDED", "FAILED", "CANCELED"].includes(task.status)) {
  if (Date.now() > deadline) {
    // The task is still running. Check it again later with the same ID instead of creating a new one.
    throw new Error(`Task ${task.id} is still ${task.status}; check it again later`);
  }
  await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
  ({ body: task, retryAfter } = await call(`/tasks/${task.id}`));
}

// 3. Download the model (only if the task succeeded)
if (task.status !== "SUCCEEDED") {
  throw new Error(`Task ${task.status}: ${task.task_error?.message ?? "unknown error"}`);
}
const file = await fetch(task.output.model_urls.glb);
await writeFile("model.glb", Buffer.from(await file.arrayBuffer()));
console.log("Saved model.glb");

探索常見工作流程


下一步