Rest api

From G1Joshi/Agent-Skills · View interactive version

Rest api is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert REST API architecture assistance covering HTTP semantics, OpenAPI/Swagger specifications, idempotent methods, hypermedia (HATEOAS), and pagination patterns. Use when designing web APIs, structuring resource endpoints, standardizing error formats, or implementing RESTful conventions.

How to use this skill

Skill file

REST API

Representational State Transfer (REST) is the architectural style for distributed hypermedia systems. It relies on stateless, client-server, cacheable communications protocols (mostly HTTP).

When to Use

Quick Start

// Express.js Example
app.get("/users/:id", async (req, res) => {
  const user = await db.find(req.params.id);
  if (!user) return res.status(404).json({ error: "Not Found" });

  // HATEOAS (Hypermedia As The Engine Of Application State) - optional but "True REST"
  res.json({
    ...user,
    links: {
      self: `/users/${user.id}`,
      orders: `/users/${user.id}/orders`,
    },
  });
});

Core Concepts

HTTP Semantics & Idempotency

Different HTTP verbs carry formal idempotency and safety guarantees:

| Method | Safe | Idempotent | Usage |

| :------- | :--- | :--------- | :------------------------------------ |

| GET | Yes | Yes | Retrieve resource representation |

| POST | No | No | Create new resource or trigger action |

| PUT | No | Yes | Replace resource entirely |

| PATCH | No | No / Yes | Partially update resource |

| DELETE | No | Yes | Remove resource |

Standardized Error Representations (RFC 7807 / 9457)

Returns machine-readable application/problem+json error responses:

{
  "type": "https://api.example.com/errors/invalid-payment",
  "title": "Invalid Payment Source",
  "status": 422,
  "detail": "The payment card provided has expired.",
  "instance": "/orders/415/payments"
}

Content Negotiation & ETag Caching

Validates whether cached client data is still fresh without resending full payloads:

app.get("/api/v1/users/:id", async (req, res) => {
  const user = await db.getUser(req.params.id);
  const etag = crypto
    .createHash("md5")
    .update(JSON.stringify(user))
    .digest("hex");

  if (req.headers["if-none-match"] === etag) {
    return res.status(304).end(); // Not Modified
  }

  res.setHeader("ETag", etag);
  res.setHeader(
    "Cache-Control",
    "public, max-age=60, stale-while-revalidate=300",
  );
  res.json(user);
});

Common Patterns

Cursor-Based Pagination

Problem: Offset-based pagination (OFFSET 10000) degrades database performance and skips items on concurrent writes.

Solution: Use monotonic cursor identifiers (e.g. created_at + id).

// GET /api/v1/posts?cursor=2026-09-27T10:00:00Z&limit=20
app.get("/api/v1/posts", async (req, res) => {
  const { cursor, limit = 20 } = req.query;
  const posts = await db
    .select()
    .from(postsTable)
    .where(
      cursor ? lt(postsTable.createdAt, new Date(cursor as string)) : undefined,
    )
    .orderBy(desc(postsTable.createdAt))
    .limit(Number(limit) + 1);

  const hasMore = posts.length > Number(limit);
  const data = hasMore ? posts.slice(0, -1) : posts;
  const nextCursor = hasMore
    ? data[data.length - 1].createdAt.toISOString()
    : null;

  res.json({ data, pagination: { hasMore, nextCursor } });
});

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------- | :------------------------------------- | :--------------------------------------------------- |

| 405 Method Not Allowed | Sending POST to a GET-only endpoint. | Check HTTP verb. |

| 415 Unsupported Media Type | Sending XML when JSON expected. | Set Content-Type: application/json. |

| CORS Error | Browser blocking cross-origin request. | Set Access-Control-Allow-Origin headers on server. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.