Gemini Developer API 可帮助您以最快的速度构建、投入生产和扩缩 Gemini 赋能的应用。大多数开发者应使用 Gemini Developer API,除非需要特定的企业控制功能。
Vertex AI 提供全面的企业级功能和服务生态系统,可用于构建和部署由 Google Cloud 平台提供支持的生成式 AI 应用。
我们最近简化了在这些服务之间迁移数据的流程。现在,您可以通过统一的 Google Gen AI SDK 访问 Gemini Developer API 和 Vertex AI Gemini API。
代码比较
本页面并排比较了 Gemini Developer API 和 Vertex AI 文本生成快速入门中的代码。
Python
您可以通过 google-genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 google-genai,请参阅库页面。
Gemini Developer API
fromgoogleimportgenaiclient=genai.Client()response=client.models.generate_content(model="gemini-2.0-flash",contents="Explain how AI works in a few words")print(response.text)
Vertex AI Gemini API
fromgoogleimportgenaiclient=genai.Client(vertexai=True,project='your-project-id',location='us-central1')response=client.models.generate_content(model="gemini-2.0-flash",contents="Explain how AI works in a few words")print(response.text)
JavaScript 和 TypeScript
您可以通过 @google/genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 @google/genai,请参阅库页面。
Gemini Developer API
import{GoogleGenAI}from"@google/genai";constai=newGoogleGenAI({});asyncfunctionmain(){constresponse=awaitai.models.generateContent({model:"gemini-2.0-flash",contents:"Explain how AI works in a few words",});console.log(response.text);}main();
Vertex AI Gemini API
import{GoogleGenAI}from'@google/genai';constai=newGoogleGenAI({vertexai:true,project:'your_project',location:'your_location',});asyncfunctionmain(){constresponse=awaitai.models.generateContent({model:"gemini-2.0-flash",contents:"Explain how AI works in a few words",});console.log(response.text);}main();
Go
您可以通过 google.golang.org/genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 google.golang.org/genai,请参阅库页面。
Gemini Developer API
import("context""encoding/json""fmt""log""google.golang.org/genai")// Your Google API keyconstapiKey="your-api-key"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,nil)iferr!=nil{log.Fatal(err)}// Call the GenerateContent method.result,err:=client.Models.GenerateContent(ctx,"gemini-2.0-flash",genai.Text("Tell me about New York?"),nil)}
Vertex AI Gemini API
import("context""encoding/json""fmt""log""google.golang.org/genai")// Your GCP projectconstproject="your-project"// A GCP location like "us-central1"constlocation="some-gcp-location"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,&genai.ClientConfig{Project:project,Location:location,Backend:genai.BackendVertexAI,})// Call the GenerateContent method.result,err:=client.Models.GenerateContent(ctx,"gemini-2.0-flash",genai.Text("Tell me about New York?"),nil)}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-22。"],[],[],null,["# Gemini Developer API v.s. Vertex AI\n\nWhen developing generative AI solutions with Gemini, Google offers two API products:\nthe [Gemini Developer API](/gemini-api/docs) and the [Vertex AI Gemini API](https://cloud.google.com/vertex-ai/generative-ai/docs/overview).\n\nThe Gemini Developer API provides the fastest path to build, productionize, and\nscale Gemini powered applications. Most developers should use the Gemini Developer\nAPI unless there is a need for specific enterprise controls.\n\nVertex AI offers a comprehensive ecosystem of enterprise ready features and services\nfor building and deploying generative AI applications backed by the Google Cloud Platform.\n\nWe've recently simplified migrating between these services. Both the Gemini\nDeveloper API and the Vertex AI Gemini API are now accessible through the unified\n[Google Gen AI SDK](/gemini-api/docs/libraries).\n\nCode comparison\n---------------\n\nThis page has side-by-side code comparisons between Gemini Developer API and\nVertex AI quickstarts for text generation.\n\n### Python\n\nYou can access both the Gemini Developer API and Vertex AI services through\nthe `google-genai` library. See the [libraries](/gemini-api/docs/libraries) page\nfor instructions on how to install `google-genai`. \n\n### Gemini Developer API\n\n from google import genai\n\n client = genai.Client()\n\n response = client.models.generate_content(\n model=\"gemini-2.0-flash\", contents=\"Explain how AI works in a few words\"\n )\n print(response.text)\n\n### Vertex AI Gemini API\n\n from google import genai\n\n client = genai.Client(\n vertexai=True, project='your-project-id', location='us-central1'\n )\n\n response = client.models.generate_content(\n model=\"gemini-2.0-flash\", contents=\"Explain how AI works in a few words\"\n )\n print(response.text)\n\n### JavaScript and TypeScript\n\nYou can access both Gemini Developer API and Vertex AI services through `@google/genai`\nlibrary. See [libraries](/gemini-api/docs/libraries) page for instructions on how\nto install `@google/genai`. \n\n### Gemini Developer API\n\n import { GoogleGenAI } from \"@google/genai\";\n\n const ai = new GoogleGenAI({});\n\n async function main() {\n const response = await ai.models.generateContent({\n model: \"gemini-2.0-flash\",\n contents: \"Explain how AI works in a few words\",\n });\n console.log(response.text);\n }\n\n main();\n\n### Vertex AI Gemini API\n\n import { GoogleGenAI } from '@google/genai';\n const ai = new GoogleGenAI({\n vertexai: true,\n project: 'your_project',\n location: 'your_location',\n });\n\n async function main() {\n const response = await ai.models.generateContent({\n model: \"gemini-2.0-flash\",\n contents: \"Explain how AI works in a few words\",\n });\n console.log(response.text);\n }\n\n main();\n\n### Go\n\nYou can access both Gemini Developer API and Vertex AI services through `google.golang.org/genai`\nlibrary. See [libraries](/gemini-api/docs/libraries) page for instructions on how\nto install `google.golang.org/genai`. \n\n### Gemini Developer API\n\n import (\n \"context\"\n \"encoding/json\"\n \"fmt\"\n \"log\"\n \"google.golang.org/genai\"\n )\n\n // Your Google API key\n const apiKey = \"your-api-key\"\n\n func main() {\n ctx := context.Background()\n client, err := genai.NewClient(ctx, nil)\n if err != nil {\n log.Fatal(err)\n }\n\n // Call the GenerateContent method.\n result, err := client.Models.GenerateContent(ctx, \"gemini-2.0-flash\", genai.Text(\"Tell me about New York?\"), nil)\n\n }\n\n### Vertex AI Gemini API\n\n import (\n \"context\"\n \"encoding/json\"\n \"fmt\"\n \"log\"\n \"google.golang.org/genai\"\n )\n\n // Your GCP project\n const project = \"your-project\"\n\n // A GCP location like \"us-central1\"\n const location = \"some-gcp-location\"\n\n func main() {\n ctx := context.Background()\n client, err := genai.NewClient(ctx, &genai.ClientConfig\n {\n Project: project,\n Location: location,\n Backend: genai.BackendVertexAI,\n })\n\n // Call the GenerateContent method.\n result, err := client.Models.GenerateContent(ctx, \"gemini-2.0-flash\", genai.Text(\"Tell me about New York?\"), nil)\n\n }\n\n### Other use cases and platforms\n\nRefer to use case specific guides on [Gemini Developer API Documentation](/gemini-api/docs)\nand [Vertex AI documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/overview)\nfor other platforms and use cases.\n\nMigration considerations\n------------------------\n\nWhen you migrate:\n\n- You'll need to use Google Cloud service accounts to authenticate. See the [Vertex AI documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/overview)\n for more information.\n\n- You can use your existing Google Cloud project\n (the same one you used to generate your API key) or you can\n [create a new Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n- Supported regions may differ between the Gemini Developer API and the\n Vertex AI Gemini API. See the list of\n [supported regions for generative AI on Google Cloud](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations-genai).\n\n- Any models you created in Google AI Studio need to be retrained in Vertex AI.\n\nIf you no longer need to use your Gemini API key for the Gemini Developer API,\nthen follow security best practices and delete it.\n\nTo delete an API key:\n\n1. Open the\n [Google Cloud API Credentials](https://console.cloud.google.com/apis/credentials)\n page.\n\n2. Find the API key you want to delete and click the **Actions** icon.\n\n3. Select **Delete API key**.\n\n4. In the **Delete credential** modal, select **Delete**.\n\n Deleting an API key takes a few minutes to propagate. After\n propagation completes, any traffic using the deleted API key is rejected.\n\n| **Important:** If you have deleted a key that is still used in production and need to recover it, see [gcloud beta services api-keys undelete](https://cloud.google.com/sdk/gcloud/reference/beta/services/api-keys/undelete).\n\nNext steps\n----------\n\n- See the [Generative AI on Vertex AI overview](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/overview) to learn more about generative AI solutions on Vertex AI."]]