MYMY 웹훅으로 콘텐츠 DB 자동 갱신하기
MYMY에서 콘텐츠가 바뀌면 웹훅이 옵니다. 웹훅에는 어떤 콘텐츠가 바뀌었는지만 들어 있어서, 받는 쪽이 MYMY API로 상세를 다시 가져와 DB에 저장합니다. 아래 코드와 샘플은 이 데모 프로젝트에서 실제로 쓰고 받은 것입니다.
외부 라이브러리 사용하지 않음
목차
①처리 흐름
직접 구현할 핵심은 STEP 3·4입니다. 요청을 받아 바로 응답하고, 콘텐츠 정보를 다시 가져와 갱신하는 부분입니다.
②웹훅 요청 예시
로컬에서 실제로 받은 요청이고, 헤더는 일부만 옮겼습니다. 본문에 콘텐츠 내용은 없고 이벤트 종류, contentId, title만 있습니다.
x-webhook-id: af80439a-b491-4ac5-b48c-dc4d97d1ad48
x-webhook-timestamp: 1794371158
x-webhook-signature: sha256=c5929a6f0aa1414f03666148df5b1e2316bb0c71b17d471fdaa1523d2ec35111
content-type: application/json{
"id": "af80439a-b491-4ac5-b48c-dc4d97d1ad48",
"event": "content.created",
"timestamp": "2026-11-11T04:25:58.191Z",
"data": {
"contentId": "2b1ff8a5a3e34eeebe83",
"title": "fallback-title"
}
}③단계별 구현
실제 소스에서 가져왔고, 줄인 부분은 // … 로 표시했습니다.
④상황별 처리
이벤트 종류는 많아도 DB를 바꾸는 처리는 두 가지뿐입니다. 최신 정보를 다시 받아 콘텐츠 정보를 갱신하거나, 숨기거나 지웁니다.
// 콘텐츠 추가·수정 (created, updated, metadata_updated, restored, workflow.completed …)
if (RESYNC_EVENTS.has(event)) {
return toOutcome([await resyncContent(contentId, data, eventId)]); // 조회·upsert 는 ③ STEP 4
}
// 인코딩 실패: 다시 조회한 뒤 실패 사실을 기록
if (event === "workflow.failed") {
if (!contentId) return { status: "ignored", error: "contentId 없음" };
const err = await resyncContent(contentId, data, eventId);
if (err) return toOutcome([err]);
db.update(contents).set({ syncError: `MYMY 워크플로 실패 (instanceId=${data.instanceId})` })
.where(eq(contents.contentId, contentId)).run();
return { status: "processed", error: null };
}
// 여러 건 수정: contentIds 마다 같은 처리 반복
if (event === "content.bulk_updated") {
for (const id of ids) results.push(await resyncContent(id, {}, eventId));
return toOutcome(results);
}
// 휴지통 이동: 상태만 trashed
if (event === "content.trashed") {
db.update(contents).set({ status: "trashed", /* … */ }).where(eq(contents.contentId, contentId)).run();
}
// 영구 삭제: 행 삭제
if (event === "content.permanently_deleted") {
db.delete(contents).where(eq(contents.contentId, contentId)).run();
}
// ping 및 그 외: 이력만 남김
return { status: "ignored", error: null };복원(content.restored), 워크플로 완료(workflow.completed), 아카이브 완료(archive.completed)도 수정과 똑같이 최신 정보를 다시 받습니다. 콘텐츠 조회 API 호출에 실패하면 수신 이력에 실패 사유가 남고, 해당 콘텐츠에는 sync_error가 기록됩니다.
⑤저장 결과
②의 웹훅으로 contents에 저장된 내용입니다. 웹훅에는 contentId와 title만 들어 있으므로, 이 contentId로 MYMY API를 호출해 메타데이터·썸네일 같은 필요한 정보를 추가로 가져옵니다.