
2Slides Team
4 min read

AI 智能体正在改变软件内容创作的方式。AI 演示智能体能够生成、定制并交付专业的幻灯片演示文稿,作为自动化工作流程的一部分——从 CRM 流程到内部报告工具,无所不能。
本指南将指导您使用 2Slides API 构建一个生产就绪的演示智能体。
一个演示智能体通常遵循以下流程:
用户请求 → 智能体逻辑 → 2Slides API → 轮询任务 → 交付结果
↓
上下文收集
(数据、模板、品牌资产)在 2slides.com 注册,并在 2slides.com/api 创建您的 API 密钥。
npm install node-fetch dotenvimport fetch from 'node-fetch';
class PresentationAgent {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'https://2slides.com/api/v1';
}
async generateSlides({ topic, themeId, language = 'en', resolution = '2K' }) {
// Step 1: Start generation
const response = await fetch(`${this.baseUrl}/slides/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
userInput: topic,
themeId,
responseLanguage: language,
resolution,
mode: 'async'
})
});
const job = await response.json();
if (!response.ok) throw new Error(job.error || 'Generation failed');
// Step 2: Poll until complete
return await this.waitForCompletion(job.jobId);
}
async waitForCompletion(jobId, maxWait = 300000) {
const start = Date.now();
while (Date.now() - start < maxWait) {
const response = await fetch(`${this.baseUrl}/jobs/${jobId}`, {
headers: { 'Authorization': `Bearer ${this.apiKey}` }
});
const status = await response.json();
if (status.status === 'success') return status;
if (status.status === 'failed') throw new Error('Generation failed');
await new Promise(r => setTimeout(r, 3000));
}
throw new Error('Generation timed out');
}
async generateFromFile({ fileUrl, prompt, themeId }) {
const response = await fetch(`${this.baseUrl}/slides/create-pdf-slides`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
fileUrl,
userInput: prompt,
themeId,
mode: 'async'
})
});
const job = await response.json();
return await this.waitForCompletion(job.jobId);
}
async generateWithDesign({ topic, referenceImageUrl, resolution = '2K' }) {
const response = await fetch(`${this.baseUrl}/slides/create-like-this`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
userInput: topic,
designStyle: {
global: { referenceImageUrl }
},
resolution,
mode: 'async'
})
});
const job = await response.json();
return await this.waitForCompletion(job.jobId);
}
async addNarration({ jobId, mode = 'single', voice = 'Charon' }) {
const response = await fetch(`${this.baseUrl}/slides/generate-narration`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ jobId, mode, voice, contentMode: 'concise' })
});
return await response.json();
}
}
// Usage
const agent = new PresentationAgent('sk-2slides-xxx');
const result = await agent.generateSlides({
topic: 'Q1 2026 Business Review',
themeId: 'mckinsey-theme-id',
resolution: '2K'
});
console.log('Download:', result.downloadUrl);import requests
import time
class PresentationAgent:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = 'https://2slides.com/api/v1'
self.headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
def generate_slides(self, topic, theme_id=None, language='en', resolution='2K'):
response = requests.post(
f'{self.base_url}/slides/generate',
headers=self.headers,
json={
'userInput': topic,
'themeId': theme_id,
'responseLanguage': language,
'resolution': resolution,
'mode': 'async'
}
)
response.raise_for_status()
job = response.json()
return self._wait_for_completion(job['jobId'])
def generate_from_file(self, file_url, prompt, theme_id=None):
response = requests.post(
f'{self.base_url}/slides/create-pdf-slides',
headers=self.headers,
json={
'fileUrl': file_url,
'userInput': prompt,
'themeId': theme_id,
'mode': 'async'
}
)
response.raise_for_status()
job = response.json()
return self._wait_for_completion(job['jobId'])
def _wait_for_completion(self, job_id, max_wait=300):
start = time.time()
while time.time() - start < max_wait:
response = requests.get(
f'{self.base_url}/jobs/{job_id}',
headers=self.headers
)
status = response.json()
if status['status'] == 'success':
return status
if status['status'] == 'failed':
raise Exception('Generation failed')
time.sleep(3)
raise TimeoutError('Generation timed out')
# Usage
agent = PresentationAgent('sk-2slides-xxx')
result = agent.generate_slides('AI Trends 2026', resolution='4K')
print(f"Download: {result['downloadUrl']}")为不同的用例预配置主题:
const TEMPLATES = {
'pitch-deck': { themeId: 'apple-id', resolution: '2K' },
'quarterly-review': { themeId: 'mckinsey-id', resolution: '2K' },
'training': { themeId: 'corporate-id', resolution: '1K' },
'social-media': { themeId: 'saul-bass-id', resolution: '4K', aspectRatio: '1:1' },
};
async function generateByType(type, topic) {
const config = TEMPLATES[type];
return agent.generateSlides({ topic, ...config });
}连接到您的数据源:
async function weeklyReport(dataSource) {
// 1. Fetch latest data
const data = await dataSource.getWeeklyMetrics();
// 2. Format as presentation input
const topic = `Weekly Report: Revenue $${data.revenue}, Users ${data.users}, Churn ${data.churn}%`;
// 3. Generate slides
const result = await agent.generateSlides({ topic, themeId: 'mckinsey-id' });
// 4. Deliver
await slackBot.postMessage('#team-updates', `Weekly report ready: ${result.downloadUrl}`);
}在一个流程中生成幻灯片 + 旁白 + 视频:
async function fullPresentation(topic) {
// 1. Generate slides
const slides = await agent.generateSlides({ topic });
// 2. Add voice narration
await agent.addNarration({ jobId: slides.jobId, mode: 'multi', voice: 'Charon' });
// 3. Download with audio
const download = await fetch(`${agent.baseUrl}/slides/download-slides-pages-voices`, {
method: 'POST',
headers: agent.headers,
body: JSON.stringify({ jobId: slides.jobId })
});
return download.json();
}是的 — 该 API 专为生产环境设计。对商业应用没有任何限制。
API 通过速率限制处理并发请求。对于高并发需求,请联系 2Slides 团队以获取企业级限制。
目前,请通过 jobs 端点进行轮询。Webhook 支持已在开发路线图上。
构建您的演示智能体 — 在 2Slides 获取您的 API 密钥 并开始以编程方式生成幻灯片。
Create stunning AI-powered presentations in seconds. Transform your ideas into professional slides with 2slides AI Agent.
Try For Free