npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@redonvn/zalo-ads-sdk

v0.2.66

Published

TypeScript SDK for Zalo Ads, Zalo Forms, Instant Pages, Profile and Widget APIs with Silent OAuth2 PKCE SSO (`zpsid`) and Multi-Integration RAM/Redis Session Manager.

Readme

@redonvn/zalo-ads-sdk

SDK TypeScript cho toàn bộ hệ sinh thái Zalo Ads (ads.zalo.me, page.ads.zalo.me, form.ads.zalo.me, profile.ads.zalo.me, api.widget.zalo.me, widget.chat.zalo.me).

Hỗ trợ:

  • Silent OAuth2 PKCE SSO (ZaloAuthManager): Chỉ cần truyền duy nhất cookie zpsid (hoặc nguyên chuỗi Cookie trình duyệt có chứa zpsid=...), SDK tự động đổi ra PLAY_SESSION, XSRF-TOKEN, zf_id và Authorization token cho từng domain.
  • Lưu trữ phiên trên RAM hoặc Redis (ZaloSessionStore):
    • MemoryZaloSessionStore: L1 RAM cache có giới hạn số lượng tài khoản theo cơ chế LRU (maxAccounts).
    • RedisZaloSessionStore: L2 Redis shared cache kèm Distributed Lock (setNX), tương thích trực tiếp với RedisService của redai-v201-be-app-staging và ioredis.
    • TieredZaloSessionStore: Kết hợp 2 tầng L1 RAM (0ms) + L2 Redis (chia sẻ giữa nhiều worker/pod + giữ phiên khi restart server).
  • Quản lý Đa Tài Khoản / Đa Integration (ZaloAdsIntegrationManager): Cô lập phiên theo integrationId (hoặc tự động tách accountKey từ zpsid), tự động phát hiện khi người dùng cập nhật zpsid mới (zpsidFingerprint) để làm mới phiên.

1. Sử dụng đơn lẻ (Mặc định tự cache trong RAM theo tài khoản)

import { ZaloAdsSdk } from "@redonvn/zalo-ads-sdk";

const sdk = new ZaloAdsSdk({
  zpsid: "g_kQ.330114198.434.TmeTxcUlEkNnYvVsQwywvHBSIDPKdmlKKvu3tSnRUwvf5VqMPj4FAmslEkK",
  // Tùy chọn: truyền thêm integrationId và redis nếu muốn lưu thẳng xuống Redis
  // integrationId: "eb18102a-...",
  // redis: this.redisService,
});

const whoami = await sdk.ads.getApiWhoami();
const rules = await sdk.ads.listAutomatedRules({ ruleName: "" });
const oas = await sdk.instantPages.listOfficialAccounts();
const formSession = await sdk.forms.getAuthSession();

2. Quản lý nhiều Integration trên Backend (redai-v201-be-app-staging) với RAM + Redis

Trên redai-v201-be-app-staging, mỗi người dùng có thể kết nối nhiều Zalo Ads Integration (integrationId). Sử dụng ZaloAdsIntegrationManager để quản lý tập trung:

import { Injectable } from "@nestjs/common";
import { RedisService } from "@shared/services/redis.service";
import { GenericIntegrationCredentialService } from "@modules/integration/shared/credentials/generic-integration-credential.service";
import { ZaloAdsIntegrationManager, ZaloAdsSdk } from "@redonvn/zalo-ads-sdk";

@Injectable()
export class ZaloAdsClientFactoryService {
  public readonly manager: ZaloAdsIntegrationManager;

  constructor(
    private readonly redisService: RedisService,
    private readonly credentialService: GenericIntegrationCredentialService,
  ) {
    this.manager = new ZaloAdsIntegrationManager({
      // Truyền trực tiếp RedisService của backend -> Tự động bật Tiered Store (L1 RAM + L2 Redis + Distributed Lock)
      // Nếu không truyền `redis`, Manager tự động dùng MemoryZaloSessionStore (RAM LRU).
      redis: this.redisService,
      redisKeyPrefix: "zalo_ads:sso",
      maxAccountsInMemory: 500,

      // Tự động lấy zpsid đã giải mã từ Credential Vault theo integrationId
      resolveCredentials: async (integrationId: string) => {
        const bound = await this.credentialService.resolveBound(integrationId);
        const creds = bound.credentials as Record<string, string>;
        return {
          zpsid: creds.zpsid || creds.cookie,
          zoawSek: creds.zoawSek,
        };
      },
    });
  }

  /** Lấy SDK thực thi cho bất kỳ integrationId nào của người dùng */
  async getSdkForIntegration(integrationId: string): Promise<ZaloAdsSdk> {
    return this.manager.forIntegration(integrationId);
  }

  /** Dùng khi người dùng bấm "Kết nối / Kiểm tra kết nối" (Auth Connect Flow) */
  async verifyAndWarmup(integrationId: string, zpsidOrCookie: string) {
    return this.manager.verifyIntegration({
      integrationId,
      zpsid: zpsidOrCookie,
    });
  }

  /** Xóa sạch phiên trong RAM + Redis khi người dùng sửa/xóa Integration */
  async invalidate(integrationId: string): Promise<void> {
    await this.manager.invalidateIntegration(integrationId);
  }
}