SDK Examples
다양한 프로그래밍 언어로 API를 사용하는 예제입니다.
Py
Python
Image/Video API
import requests
import time
API_KEY = "cdt_your_api_key"
BASE_URL = "https://api.core.today/v1"
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
# Create prediction
response = requests.post(
f"{BASE_URL}/predictions",
headers=headers,
json={
"model": "flux-schnell",
"input": {"prompt": "A cute cat wearing a hat"}
}
)
job_id = response.json()["job_id"]
# Wait for result
while True:
result = requests.get(
f"{BASE_URL}/predictions/{job_id}",
headers=headers
).json()
if result["status"] == "completed":
print(f"Image URL: {result['result'][0]}")
break
elif result["status"] == "failed":
print(f"Error: {result.get('error')}")
break
time.sleep(2)LLM API (OpenAI SDK)
from openai import OpenAI
client = OpenAI(
api_key="cdt_your_api_key",
base_url="https://api.core.today/llm/openai/v1"
)
# Chat completion
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
# Streaming
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")JS
JavaScript / TypeScript
Image/Video API
const API_KEY = "cdt_your_api_key";
const BASE_URL = "https://api.core.today/v1";
async function generateImage(prompt) {
// Create prediction
const createResponse = await fetch(`${BASE_URL}/predictions`, {
method: "POST",
headers: {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "flux-schnell",
input: { prompt }
})
});
const { job_id } = await createResponse.json();
// Poll for result
while (true) {
const result = await fetch(`${BASE_URL}/predictions/${job_id}`, {
headers: { "X-API-Key": API_KEY }
}).then(r => r.json());
if (result.status === "completed") {
return result.result[0];
} else if (result.status === "failed") {
throw new Error(result.error);
}
await new Promise(r => setTimeout(r, 2000));
}
}
const imageUrl = await generateImage("A beautiful landscape");LLM API
const API_KEY = "cdt_your_api_key";
async function chat(message) {
const response = await fetch(
"https://api.core.today/llm/openai/v1/chat/completions",
{
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-4o",
messages: [{ role: "user", content: message }]
})
}
);
const data = await response.json();
return data.choices[0].message.content;
}
const reply = await chat("Hello!");
console.log(reply);