ابنِ باستخدام واجهة برمجة CookMyPDF
كل أداة في هذا الموقع يمكن استدعاؤها من شيفرتك. 300 رصيد مجاني في الشهر، ومهمة واحدة برصيد واحد. أنشئ مفتاح API من صفحة حسابك.
البدء السريع
ثلاثة طلبات: احصل على رابط رفع موقّع مسبقاً، وأنشئ مهمة، ونزّل النتيجة. صادِق بالترويسة Authorization: Bearer ff_live_….
# 1. Get an upload URL, saying how large the file is in bytes.
# The URL is signed for exactly that size; storage refuses any other.
curl -X POST https://api.cookmypdf.com/v1/uploads \
-H "Authorization: Bearer ff_live_..." \
-H "Content-Type: application/json" -d '{"files": [{"size": 48213}]}'
# 2. PUT that file to the returned url, then create the job
curl -X POST https://api.cookmypdf.com/v1/jobs \
-H "Authorization: Bearer ff_live_..." \
-H "Content-Type: application/json" \
-d '{"toolId": "compress-pdf", "inputKeys": ["up/<key>"], "options": {"level": "recommended"}}'
# 3. Poll until succeeded, then fetch download links
curl https://api.cookmypdf.com/v1/jobs/<id> -H "Authorization: Bearer ff_live_..."
curl https://api.cookmypdf.com/v1/jobs/<id>/download -H "Authorization: Bearer ff_live_..."Node.js
const API = 'https://api.cookmypdf.com';
const KEY = process.env.COOKMYPDF_API_KEY;
const headers = { authorization: `Bearer ${KEY}`, 'content-type': 'application/json' };
// Each upload URL is signed for one size; storage refuses any other.
const { uploads } = await (await fetch(`${API}/v1/uploads`, {
method: 'POST', headers, body: JSON.stringify({ files: [{ size: fileBytes.byteLength }] }),
})).json();
await fetch(uploads[0].url, { method: 'PUT', body: fileBytes });
const { id } = await (await fetch(`${API}/v1/jobs`, {
method: 'POST', headers,
body: JSON.stringify({ toolId: 'pdf-to-word', inputKeys: [uploads[0].key] }),
})).json();
let job;
do {
await new Promise(r => setTimeout(r, 700));
job = await (await fetch(`${API}/v1/jobs/${id}`, { headers })).json();
} while (job.status === 'queued' || job.status === 'processing');
const { urls } = await (await fetch(`${API}/v1/jobs/${id}/download`, { headers })).json();Python
import os, time, requests
API = "https://api.cookmypdf.com"
HEADERS = {"Authorization": f"Bearer {os.environ['COOKMYPDF_API_KEY']}"}
size = os.path.getsize("input.pdf") # each upload URL is signed for exactly this many bytes
uploads = requests.post(f"{API}/v1/uploads", json={"files": [{"size": size}]}, headers=HEADERS).json()["uploads"]
requests.put(uploads[0]["url"], data=open("input.pdf", "rb"))
job = requests.post(f"{API}/v1/jobs", headers=HEADERS, json={
"toolId": "compress-pdf", "inputKeys": [uploads[0]["key"]],
}).json()
while True:
status = requests.get(f"{API}/v1/jobs/{job['id']}", headers=HEADERS).json()
if status["status"] in ("succeeded", "failed"):
break
time.sleep(0.7)
urls = requests.get(f"{API}/v1/jobs/{job['id']}/download", headers=HEADERS).json()["urls"]خطافات الويب
مرّر webhookUrl (بروتوكول https) عند إنشاء المهمة، فنرسل إليك النتيجة بطلب POST حين تنتهي، موقّعة بسر خطاف الويب الخاص بمفتاحك لتثبت أنها جاءت منا:
// Verify X-CookMyPDF-Signature: t=<unix>,v1=<hex>
import { createHmac, timingSafeEqual } from 'node:crypto';
function verify(body, header, webhookSecret) {
const [t, v1] = [/t=(\d+)/, /v1=([0-9a-f]+)/].map(re => re.exec(header)?.[1]);
if (!t || !v1 || Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;
const expected = createHmac('sha256', webhookSecret).update(`${t}.${body}`).digest('hex');
return expected.length === v1.length && timingSafeEqual(Buffer.from(expected), Buffer.from(v1));
}المرجع
- حزم تطوير رسمية:
npm install @cookmypdf/sdk·pip install cookmypdf، وكلتاهما تغلّف تسلسل الرفع والاستعلام والتنزيل في استدعاء واحد وتتحقق من توقيعات خطافات الويب نيابةً عنك - مواصفة OpenAPI 3.1 الكاملة: /v1/openapi.json
- كل أداة وخياراتها: مرجع الأدوات، وهو متاح للآلات في /v1/tools
- تحمل المفاتيح صلاحيات (
jobs:readوjobs:writeوaiوsign:write) وحداً للطلبات لكل مفتاح. امنح ما يحتاجه التكامل فقط: فالمفتاح بلاaiلا يستطيع إنفاق رصيدك من الذكاء الاصطناعي. الصلاحية الناقصة تُرجع403؛ وتجاوز الحد يُرجع429 - الرصيد: المجانية 300 في الشهر · Premium 2,000 في الشهر · Business 10,000 في الشهر، انظر الأسعار
- تُحذف الملفات تلقائياً خلال ساعة واحدة؛ وتبقى روابط التنزيل 15 دقيقة، انظر الخصوصية والأمان
- حالة الخدمة: صفحة الحالة