← 홈으로 돌아가기

API 사용 예제

단축 URL 생성 API 엔드포인트는 POST /api/shorten 입니다.

로그인 사용자는 API 키를 Authorization: Bearer <YOUR_API_KEY> 헤더에 포함해야 합니다.

1) 로그인 사용자 예제 (r3 타입)

curl -X POST "https://api.gate1253.workers.dev/api/shorten" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY_HERE" \
  -d '{
    "url":"https://example.com/long/path/to/resource",
    "alias":"mycustomcode",
    "type": "r3"
  }'

응답 예시 (커스텀 코드):

{
  "ok": true,
  "code": "YOUR_UNIQUE_USER_ID/mycustomcode",
  "shortUrl": "https://r3.ggm.kr/YOUR_UNIQUE_USER_ID/mycustomcode",
  "message": "단축 URL이 생성되었습니다."
}

1.1) 비회원 예제 (r3 타입)

curl -X POST "https://api.gate1253.workers.dev/api/shorten" \
  -H "Content-Type: application/json" \
  -d '{
    "url":"https://example.com/another/long/path",
    "type": "r3"
  }'

응답 예시 (무작위 코드):

{
  "ok": true,
  "code": "a1B2c3",
  "shortUrl": "https://r3.ggm.kr/a1B2c3",
  "message": "단축 URL이 생성되었습니다."
}

1.2) 로그인 사용자 예제 (R1 타입 - 만료 시간 지정)

curl -X POST "https://api.gate1253.workers.dev/api/shorten" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY_HERE" \
  -d '{
    "url":"https://example.com/secret-document",
    "type": "r1",
    "expiresAt": "2025-01-01T00:00:00Z"
  }'

응답 예시 (R1 타입):

{
  "ok": true,
  "code": "YOUR_UNIQUE_USER_ID/a1B2c3",
  "shortUrl": "https://r1.ggm.kr/YOUR_UNIQUE_USER_ID/a1B2c3",
  "message": "단축 URL이 생성되었습니다."
}

2) JavaScript (fetch) 예제 (r2 타입)

const API_BASE = 'https://api.gate1253.workers.dev';
const YOUR_API_KEY = 'YOUR_API_KEY_HERE'; // 실제 API 키로 교체

async function shortenWithCustomCode(originalUrl, customAlias) {
  const headers = { 'Content-Type': 'application/json' };
  if (YOUR_API_KEY) {
    headers['Authorization'] = `Bearer ${YOUR_API_KEY}`;
  }

  const body = {
    url: originalUrl,
    alias: customAlias,
    type: 'r2'
  };

  const res = await fetch(`${API_BASE}/api/shorten`, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(body)
  });
  return res.json();
}

// 사용 예:
shortenWithCustomCode('https://example.com/long/path/to/resource', 'my-secure-link')
  .then(data => console.log(data))
  .catch(err => console.error(err));

2.1) JavaScript (fetch) 예제 (r3 타입)

const API_BASE = 'https://api.gate1253.workers.dev';

async function shortenAnonymous(originalUrl) {
  const headers = { 'Content-Type': 'application/json' };
  const body = { 
    url: originalUrl,
    type: 'r3' // 타입은 선택사항이며, 기본값은 'r3' 입니다.
  };

  const res = await fetch(`${API_BASE}/api/shorten`, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(body)
  });
  return res.json();
}

// 사용 예:
shortenAnonymous('https://example.com/another/long/path')
  .then(data => console.log(data))
  .catch(err => console.error(err));

참고: 브라우저에서 다른 오리진으로 호출할 경우 CORS preflight(OPTIONS)가 발생합니다. 서비스는 기본적으로 모든 오리진을 허용하도록 설정되어 있지만, 필요 시 도메인 제한을 적용하세요.

2.2) JavaScript (fetch) 예제 (R1 타입 - 만료 시간 지정)

const API_BASE = 'https://api.gate1253.workers.dev';
const YOUR_API_KEY = 'YOUR_API_KEY_HERE'; // 실제 API 키로 교체

async function shortenWithExpiration(originalUrl, expirationDateTime) {
  const headers = { 
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${YOUR_API_KEY}`
  };

  const body = { 
    url: originalUrl,
    type: 'r1',
    expiresAt: expirationDateTime // 예: "2025-01-01T00:00:00Z" (UTC 시간)
  };

  const res = await fetch(`${API_BASE}/api/shorten`, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(body)
  });
  return res.json();
}

// 사용 예:
shortenWithExpiration('https://example.com/secret-document', '2025-01-01T00:00:00')
  .then(data => console.log(data))
  .catch(err => console.error(err));

2.3) JavaScript (fetch) 예제 (R5 타입)

const API_BASE = 'https://api.gate1253.workers.dev';

async function shortenForLargeFile(originalUrl) {
  const headers = { 'Content-Type': 'application/json' };
  const body = { url: originalUrl, type: 'r5' };

  const res = await fetch(`${API_BASE}/api/shorten`, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(body)
  });
  return res.json();
}

3) 페이지 내 데모

추가 안내