Skip to content

Cred-X/credx-api

Repository files navigation

CredX API

A Cloudflare Workers API built with Hono, Drizzle ORM, Turso (LibSQL), and Upstash Redis.

Prerequisites

Setup

1. Clone the Repository

git clone <repository-url>
cd credX-api

2. Install Dependencies

npm install
# or
bun install

3. Configure Environment Variables

Create a .env file in the root directory:

TURSO_DATABASE_URL=your-turso-database-url
TURSO_AUTH_TOKEN=your-turso-auth-token
REDIS_URL=your-upstash-redis-url
REDIS_TOKEN=your-upstash-redis-token
FRONTEND_URL=your-frontend-url

Getting the credentials:

  • Turso Database:
    • Sign up at turso.tech
    • Create a new database
    • Get the URL and auth token from the Turso dashboard
  • Upstash Redis:
    • Sign up at upstash.com
    • Create a new Redis database
    • Copy the REST URL and token from the database details

4. Update wrangler.jsonc

Update the vars section in wrangler.jsonc with your values:

{
	"vars": {
		"TURSO_DATABASE_URL": "your-turso-database-url",
		"TURSO_AUTH_TOKEN": "your-turso-auth-token",
		"REDIS_URL": "your-upstash-redis-url",
		"REDIS_TOKEN": "your-upstash-redis-token",
		"FRONTEND_URL": "your-frontend-url"
	}
}

5. Generate Database Schema

Generate and push the database schema to Turso:

npm run drizzle:generate
npm run drizzle:push

6. Generate Cloudflare Types

Generate TypeScript types based on your Worker configuration:

npm run cf-typegen

Development

Start the development server:

npm run dev

The API will be available at http://localhost:8787

Deployment

Deploy to Cloudflare Workers:

npm run deploy

Available Scripts

  • npm run dev - Start development server with Wrangler
  • npm run build - Build the Worker
  • npm run deploy - Deploy to Cloudflare Workers (minified)
  • npm run cf-typegen - Generate CloudflareBindings types
  • npm run drizzle:generate - Generate Drizzle migration files
  • npm run drizzle:push - Push schema changes to database

Usage with Hono

Pass the CloudflareBindings as generics when instantiating Hono:

// src/index.ts
const app = new Hono<{ Bindings: CloudflareBindings }>();

Technologies

License

This project is licensed under the MIT License - see the LICENSE file for details.