@guardian/google-admanager-api

中文文档
8.0.2发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 6967

Google Ad Manager API Client Library for NodeJs

原始使用文档
文档目录6 节
  1. Description
  2. Installing the library
  3. Overview
  4. Validation
  5. Debugging
  6. Known issues/gotchas

Ad Manager Logo

Google Ad Manager API Client Library for NodeJs.

NPM Version Package License NPM Downloads

Description

Forked from https://github.com/Niurmiguel/google-admanager-api as the original package was not maintained.

Developers can use the Google Ad Manager API to build applications that manage inventory, create orders, pull reports, and more.

Installing the library

$ npm install @guardian/google-admanager-api
$ yarn add @guardian/google-admanager-api
$ pnpm add @guardian/google-admanager-api

Overview

Authentication

All Google Ad Manager API calls must be authorized through OAuth2 an open standard that allows users to grant permissions to third-party applications, so the application can interact with web services on the user's behalf. OAuth2 enables your Ad Manager API client application to access a user's Ad Manager account without having to handle or store the user's username or password.

Generate service account credentials

const credential = new GoogleSACredential({
    type: "service_account",
    project_id: "...",
    private_key_id: "...",
    private_key: "...",
    client_email: "...",
    client_id: "...",
    auth_uri: "...",
    token_uri: "...",
    ...
});

//or

const credential = new GoogleSAFileCredential('./credentials.json');

Generate user account credentials

Using the API on behalf of a real user is slightly more complicated than using a service account. The user must grant your application access to their Ad Manager account. This is done by redirecting the user to Google's OAuth2 consent screen, where they will be asked to grant your application access to their Ad Manager account.

There is an example of how to generate user account credentials in the example folder.

It uses the refresh token generation example to generate the refresh token.

There is also an option to use an access token directly. As access tokens are short-lived, you will need to refresh them yourself.

Use a client library

const adManagerClient = new AdManagerClient('networkCode',credential,'applicationName');

const orderService = await adManagerClient.getService("OrderService");
const statement = new StatementBuilder().limit(10);
const orderPage = await orderService.getOrdersByStatement(statement.toStatement())

/**
 * {
 *  results: [],
 *  totalResultSetSize: 0,
 *  startIndex: 0
 * }
 * /
Options
networkCode Number The network code of the network being addressed (required).
credential SACredential OAuth2 credential (required).
applicationName String An arbitrary string name identifying your application. This will be shown in Google's log files. For example: "My Inventory Application" or "App_1" (optional).
apiVersion String Ad Mananger API version, if you want to define a different version than the default version, i.e. v202605 (optional).

Validation

Using Superstruct for type validation so it could be used to parse the line items data we get back from the API in different Guardian projects.

Each type will have its own Struct which should be found in the same file as the type and then they will be used in the lineItem.struct.ts.

Having a type safety check in an early stage reduces bugs that we might encounter later and there is no better place than this repository to add it.

Debugging

Enable request and response logging by setting logRequests and/or logResponses to true on the service object.

const orderService = await adManagerClient.getService("OrderService");
orderService.logRequests = true;
orderService.logResponses = true;

Known issues/gotchas

SOAP requires object properties to be in the correct order

The properties of objects sent using this library need to be in the correct order (the same order as the type definitions) or you may encounter an Unmarshalling Error response.

For example for a line item the orderName must be before the startDateTime.

// will not work
lineItemService.createLineitem({
  priority: 12,
  orderId: 123,
  ...
})

// must be
lineItemService.createLineitem({
  orderId: 123,
  priority: 12,
  ...
})

Some objects need additional attributes

In some cases where multiple shapes of objects are accepted, the type need to be specified under a special attributes property.

For example, for custom targeting, the objects need to have their type specified whether they are a CustomCriteriaSet or CustomTargeting like so:

const customTargeting: CustomCriteriaSet = {
  attributes: {
    "xsi:type": "CustomCriteriaSet",
  },
  logicalOperator: LogicalOperator.OR,
  children: [{
    attributes: {
      "xsi:type": "CustomCriteriaSet",
    },
    logicalOperator: LogicalOperator.AND,
    children: [{
      attributes: {
        "xsi:type": "CustomCriteria",
      },
      keyId: 123,
      valueIds: [123],
      operator: ComparisonOperator.IS,
    }],
  }],
};

lineItemService.createLineitem({
  ...
  targeting: {
    customTargeting
  }
  ...
})

It also applies to technology targeting, as there are many types of technologies

type Browser = {
  attributes: {
    "xsi:type": "Browser";
  };
  id: number;
  name: string;
  majorVersion: string;
  minorVersion: string;
};

版本与发布记录

@guardian/google-admanager-api 共发布 88 个版本,首次发布于 2024/08/20;过去 12 个月发布了 24 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.2latest2026/10/091 个月前
8.0.12026/09/102 个月前
8.0.02026/08/103 个月前
0.0.0-beta-20260803150447beta2026/08/033 个月前
7.0.52026/07/233 个月前
7.0.42026/07/084 个月前

关于 @guardian/google-admanager-api 的常见问题

@guardian/google-admanager-api 是什么?

Google Ad Manager API Client Library for NodeJs

如何安装 @guardian/google-admanager-api?

在项目目录运行 npm install @guardian/google-admanager-api,或使用 pnpm add / yarn add / bun add。

@guardian/google-admanager-api 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@guardian/google-admanager-api 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@guardian/google-admanager-api 可以商用吗?

@guardian/google-admanager-api 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@guardian/google-admanager-api 的下载量有多少?

根据 npm 官方下载统计,@guardian/google-admanager-api 在 2026/10/17 – 2026/11/15 这 30 天内被下载 6,967 次,日均约 232 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@guardian/google-admanager-api 还在维护吗?

最新版本 8.0.2 发布于 2026/10/09(1 个月前);至今共发布 88 个版本,其中过去 12 个月发布了 24 个;首次发布于 2024/08/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。