The SDK supports three main ways to handle authentication with Google Cloud:
1. Default Authentication
Provide the region and projectId in the constructor. The client will use the default Google Cloud authentication flow (e.g., Application Default Credentials).
2. Custom GoogleAuth Configuration
Pass a googleAuth instance from the google-auth-library to specify custom scopes or a specific keyFile (service account JSON).
3. Pre-configured AuthClient
For advanced scenarios like service account impersonation, you can pass a pre-configured authClient directly to the constructor.
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
import { GoogleAuth } from 'google-auth-library';
// --- Default Authentication ---
const clientDefault = new AnthropicVertex({
region: 'us-central1',
projectId: 'my-project-id',
});
// --- Custom GoogleAuth configuration ---
const clientCustomAuth = new AnthropicVertex({
googleAuth: new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
keyFile: '/path/to/service-account.json',
}),
region: 'us-central1',
projectId: 'my-project-id',
});
// --- Pre-configured AuthClient (e.g., Impersonation) ---
import { Impersonated } from 'google-auth-library';
const authClient = new Impersonated({
sourceClient: await new GoogleAuth().getClient(),
targetPrincipal: 'impersonated-account@projectID.iam.gserviceaccount.com',
lifetime: 30,
delegates: [],
targetScopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
const clientImpersonated = new AnthropicVertex({
authClient,
region: 'us-central1',
projectId: 'my-project-id',
});