Skip to content
Skillpedia
APIs & servers (backend)48k Β· #4569

expo-api-routes

What it does

This tool lets you create 'API routes' in your Expo app. API routes are like small programs that run on a server to handle tasks that can't or shouldn't be done directly on the phone, like storing secrets or talking to other services.

When to use it

  • βœ“When you need to store secret information (passwords, API keys) that cannot go to the phone.
  • βœ“To perform direct database operations without exposing the details.
  • βœ“To communicate with other online services (like OpenAI or Stripe) securely.

Skill link

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/expo-api-routes/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/expo-api-routes/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œCreate an API route to fetch user data and protect it with a secret key.”

5 new skills a week. 1 minute to read.

How it works

You create special files named `+api.ts` inside the `app` folder. Each file can respond to different types of requests (like viewing data, creating data, etc.) using functions like `GET`, `POST`, `PUT`, and `DELETE`.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

When to Use API Routes

Use API routes when you need:

  • Server-side secrets β€” API keys, database credentials, or tokens that must never reach the client
  • Database operations β€” Direct database queries that shouldn't be exposed
  • Third-party API proxies β€” Hide API keys when calling external services (OpenAI, Stripe, etc.)
  • Server-side validation β€” Validate data before database writes
  • Webhook endpoints β€” Receive callbacks from services like Stripe or GitHub
  • Rate limiting β€” Control access at the server level
  • Heavy computation β€” Offload processing that would be slow on mobile

When NOT to Use API Routes

Avoid API routes when:

  • Data is already public β€” Use direct fetch to public APIs instead
  • No secrets required β€” Static data or client-safe operations
  • Real-time updates needed β€” Use WebSockets or services like Supabase Realtime
  • Simple CRUD β€” Consider Firebase, Supabase, or Convex for managed backends
  • File uploads β€” Use direct-to-storage uploads (S3 presigned URLs, Cloudflare R2)
  • Authentication only β€” Use Clerk, Auth0, or Firebase Auth instead

File Structure

API routes live in the app directory with +api.ts suffix:

app/
  api/
    hello+api.ts          β†’ GET /api/hello
    users+api.ts          β†’ /api/users
    users/[id]+api.ts     β†’ /api/users/:id
  (tabs)/
    index.tsx

Basic API Route

// app/api/hello+api.ts
export function GET(request: Request) {
  return Response.json({ message: "Hello from Expo!" });
}

HTTP Methods

Export named functions for each HTTP method:

// app/api/items+api.ts
export function GET(request: Request) {
  return Response.json({ items: [] });
}

export async function POST(request: Request) {
  const body = await request.json();
  return Response.json({ created: body }, { status: 201 });
}

export async function PUT(request: Request) {
  const body = await request.json();
  return Response.json({ updated: body });
}

export async function DELETE(request: Request) {
  return new Response(null, { status: 204 });
}

Dynamic Routes

// app/api/users/[id]+api.ts
export function GET(request: Request, { id }: { id: string }) {
  return Response.json({ userId: id });
}

Request Handling

Query Parameters

export function GET(request: Request) {
  const url = new URL(request.url);
  const page = url.searchParams.get("page") ?? "1";
  const limit = url.searchParams.get("limit") ?? "10";

  return Response.json({ page, limit });
}

Headers

export function GET(request: Request) {
  const auth = request.headers.get("Authorization");

  if (!auth) {
    return Response.json({ error: "Unauthorized" }, { status: 401 });
  }

  return Response.json({ authenticated: true });
}

JSON Body

export async function POST(request: Request) {
  const { email, password } = await request.json();

  if (!email || !password) {
    return Response.json({ error: "Missing fields" }, { status: 400 });
  }

  return Response.json({ success: true });
}

Environment Variables

Use process.env for server-side secrets:

// app/api/ai+api.ts
export async function POST(request: Request) {
  const { prompt } = await request.json();

  const response = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
    },
    body: JSON.stringify({
      model: "gpt-4",
      messages: [{ role: "user", content: prompt }],
    }),
  });

  const data = await response.json();
  return Response.json(data);
}

Set environment variables:

  • Local: Create .env file (never commit)
  • EAS Hosting: Use eas env:create or Expo dashboard

CORS Headers

Add CORS for web clients:

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
  "Access-Control-Allow-Headers": "Content-Type, Authorization",
};

export function OPTIONS() {
  return new Response(null, { headers: corsHeaders });
}

export function GET() {
  return Response.json({ data: "value" }, { headers: corsHeaders });
}

Error Handling

export async function POST(request: Request) {
  try {
    const body = await request.json();
    // Process...
    return Response.json({ success: true });
  } catch (error) {
    console.error("API error:", error);

…
FAQ

What are 'API routes'?

They are like 'doors' in your app that, instead of showing a screen, run code on a server to perform specific tasks, like fetching or saving information securely.

Where do I put these `+api.ts` files?

They go inside the `app` folder of your project, usually in a subfolder named `api`.

Can I use this for everything?

No. If the data is public, or you need real-time updates, or it's something simple like authentication, there are better tools. This is for tasks that need a 'brain' on the server.

Details

Original description by the author

Guidelines for creating API routes in Expo Router with EAS Hosting

repo: sickn33/agentic-awesome-skills

Popularity

GitHub stars
48k
Ranking position
#4569
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills