Langsung ke konten
Skillpedia
Antarmuka (UI/UX)48k Β· #3764

api-and-interface-design

Fungsinya

Panduan ini membantu membuat interface (seperti API atau kontrak antar bagian program) yang mudah digunakan dan sulit disalahgunakan. Panduan ini mengajarkan cara mendefinisikan dengan jelas dan stabil bagaimana bagian-bagian perangkat lunak yang berbeda akan berkomunikasi.

Kapan dipakai

  • βœ“Saat membuat koneksi (API) baru untuk program Anda.
  • βœ“Saat mendefinisikan bagaimana bagian-bagian kode Anda akan 'berbicara' satu sama lain.
  • βœ“Saat merencanakan bagaimana sebuah website (frontend) akan meminta informasi dari server (backend).

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/api-and-interface-design/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/api-and-interface-design/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana cara membuat API untuk mengelola daftar tugas, dengan memastikan error ditampilkan dengan jelas?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Caranya dengan mendefinisikan 'kontrak' yang jelas sebelum menulis kode. Kontrak ini menentukan secara tepat bagaimana komunikasi harus terjadi, termasuk cara menangani error dan informasi apa yang diharapkan.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

API and Interface Design

Overview

Design stable, well-documented interfaces that are hard to misuse. Good interfaces make the right thing easy and the wrong thing hard. This applies to REST APIs, GraphQL schemas, module boundaries, component props, and any surface where one piece of code talks to another.

When to Use

  • Designing new API endpoints
  • Defining module boundaries or contracts between teams
  • Creating component prop interfaces
  • Establishing database schema that informs API shape
  • Changing existing public interfaces

Core Principles

Hyrum's Law

With a sufficient number of users of an API, all observable behaviors of your system will be depended on by somebody, regardless of what you promise in the contract.

This means: every public behavior β€” including undocumented quirks, error message text, timing, and ordering β€” becomes a de facto contract once users depend on it. Design implications:

  • Be intentional about what you expose. Every observable behavior is a potential commitment.
  • Don't leak implementation details. If users can observe it, they will depend on it.
  • Plan for deprecation at design time. See deprecation-and-migration for how to safely remove things users depend on.
  • Tests are not enough. Even with perfect contract tests, Hyrum's Law means "safe" changes can break real users who depend on undocumented behavior.

The One-Version Rule

Avoid forcing consumers to choose between multiple versions of the same dependency or API. Diamond dependency problems arise when different consumers need different versions of the same thing. Design for a world where only one version exists at a time β€” extend rather than fork.

1. Contract First

Define the interface before implementing it. The contract is the spec β€” implementation follows.

// Define the contract first
interface TaskAPI {
  // Creates a task and returns the created task with server-generated fields
  createTask(input: CreateTaskInput): Promise<Task>;

  // Returns paginated tasks matching filters
  listTasks(params: ListTasksParams): Promise<PaginatedResult<Task>>;

  // Returns a single task or throws NotFoundError
  getTask(id: string): Promise<Task>;

  // Partial update β€” only provided fields change
  updateTask(id: string, input: UpdateTaskInput): Promise<Task>;

  // Idempotent delete β€” succeeds even if already deleted
  deleteTask(id: string): Promise<void>;
}

2. Consistent Error Semantics

Pick one error strategy and use it everywhere:

// REST: HTTP status codes + structured error body
// Every error response follows the same shape
interface APIError {
  error: {
    code: string;        // Machine-readable: "VALIDATION_ERROR"
    message: string;     // Human-readable: "Email is required"
    details?: unknown;   // Additional context when helpful
  };
}

// Status code mapping
// 400 β†’ Client sent invalid data
// 401 β†’ Not authenticated
// 403 β†’ Authenticated but not authorized
// 404 β†’ Resource not found
// 409 β†’ Conflict (duplicate, version mismatch)
// 422 β†’ Validation failed (semantically invalid)
// 500 β†’ Server error (never expose internal details)

Don't mix patterns. If some endpoints throw, others return null, and others return { error } β€” the consumer can't predict behavior.

3. Validate at Boundaries

Trust internal code. Validate at system edges where external input enters:

// Validate at the API boundary
app.post('/api/tasks', async (req, res) => {
  const result = CreateTaskSchema.safeParse(req.body);
  if (!result.success) {
    return res.status(422).json({
      error: {
        code: 'VALIDATION_ERROR',
        message: 'Invalid task data',
        details: result.error.flatten(),
      },
    });
  }

  // After validation, internal code trusts the types
  const task = await taskService.create(result.data);
  return res.status(201).json(task);
});

Where validation belongs:

  • API route handlers (user input)
  • Form submission handlers (user input)
  • External service response parsing (third-party data -- always treat as untrusted)
  • Environment variable loading (configuration)

Third-party API responses are untrusted data. Validate their shape and content before using them in any logic, rendering, or decision-making. A compromised or misbehaving external service can return unexpected types, malicious content, or instruction-like text.

Where validation does NOT belong:

…

Pertanyaan umum

Apa itu 'interface' dalam konteks ini?

Interface itu seperti kesepakatan atau peta yang mendefinisikan bagaimana dua bagian program (atau program dengan program lain) akan berinteraksi, termasuk informasi apa yang boleh dipertukarkan dan caranya.

Mengapa penting punya 'kontrak' sebelum memprogram?

Supaya semua orang paham bagaimana komunikasi seharusnya berjalan, sehingga kebingungan dan error bisa dicegah. Dengan begitu, implementasinya (kode yang sebenarnya) mengikuti rencana.

Apa itu 'Hyrum's Law'?

Hukum ini menyatakan bahwa jika banyak orang menggunakan interface Anda, mereka mungkin akan bergantung pada perilaku apa pun yang mereka lihat, meskipun perilaku itu tidak dimaksudkan. Karena itu, penting untuk berhati-hati tentang apa yang Anda tampilkan.

Detail

Deskripsi asli dari pembuat

Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#3764
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa