技能 编程开发 处理Hootsuite API限速与重试

处理Hootsuite API限速与重试

v20260423
hootsuite-rate-limits
本技能指导如何处理与Hootsuite API交互时遇到的速率限制问题(429错误)。它演示了最佳实践,包括实现健壮的重试逻辑、使用`Retry-After`头进行指数退避,并通过队列调度来管理API请求,确保调用稳定高效,避免因超出配额而失败。
获取技能
367 次下载
概览

Hootsuite Rate Limits

Overview

Handle Hootsuite API rate limits. The API returns 429 Too Many Requests with Retry-After headers when limits are exceeded.

Rate Limits

Endpoint Limit Window
General API Varies by plan Per minute
Message scheduling ~100/hour Per hour
Media upload ~50/hour Per hour
Token refresh ~10/hour Per hour

Instructions

Step 1: Respect Retry-After Header

async function rateLimitedRequest(url: string, options: RequestInit = {}) {
  const response = await fetch(url, {
    ...options,
    headers: { 'Authorization': `Bearer ${process.env.HOOTSUITE_ACCESS_TOKEN}`, ...options.headers },
  });

  if (response.status === 429) {
    const retryAfter = parseInt(response.headers.get('Retry-After') || '60');
    console.log(`Rate limited. Retrying in ${retryAfter}s`);
    await new Promise(r => setTimeout(r, retryAfter * 1000));
    return rateLimitedRequest(url, options); // Retry
  }

  return response;
}

Step 2: Queue-Based Scheduling

import PQueue from 'p-queue';

const hootsuiteQueue = new PQueue({
  concurrency: 1,
  interval: 1000,
  intervalCap: 2, // 2 requests per second
});

async function queuedSchedule(profileId: string, text: string, time: Date) {
  return hootsuiteQueue.add(() =>
    fetch('https://platform.hootsuite.com/v1/messages', {
      method: 'POST',
      headers: { 'Authorization': `Bearer ${process.env.HOOTSUITE_ACCESS_TOKEN}`, 'Content-Type': 'application/json' },
      body: JSON.stringify({ text, socialProfileIds: [profileId], scheduledSendTime: time.toISOString() }),
    })
  );
}

Resources

Next Steps

For security, see hootsuite-security-basics.

信息
Category 编程开发
Name hootsuite-rate-limits
版本 v20260423
大小 2.33KB
更新时间 2026-04-28
语言