Complete step-by-step guide to deploy Apex CLI so others can use it.
- Prerequisites
- Project Structure Overview
- Environment Setup
- Database Setup (PostgreSQL)
- Server Deployment
- Client Deployment
- NPM Package Publishing
- User Installation Guide
- Troubleshooting
Before deploying, ensure you have:
- Node.js v18+
- npm v9+ or pnpm
- Git installed
- PostgreSQL database (local or cloud)
- Google Cloud Account (for Gemini API)
- GitHub/Google OAuth Apps (for authentication)
| Service | Purpose | Recommended Provider |
|---|---|---|
| Database | PostgreSQL | Neon, Supabase, Railway |
| Server | Express API | Railway, Render, Fly.io |
| Client | Next.js Frontend | Vercel, Netlify |
| NPM | CLI Package | npmjs.com |
Apex-cli/
├── client/ # Next.js frontend (for OAuth flow)
│ ├── app/
│ │ ├── (auth)/ # Sign-in/Sign-up pages
│ │ ├── approve/ # Device approval page
│ │ └── device/ # Device authorization page
│ └── ...
├── server/ # Express backend + CLI
│ ├── src/
│ │ ├── cli/ # CLI source code
│ │ ├── lib/ # Auth, DB, utilities
│ │ └── index.js # Express server
│ ├── prisma/ # Database schema
│ └── package.json # NPM package config
└── docs/ # Documentation
- Go to Google AI Studio
- Click "Create API Key"
- Copy the key →
GOOGLE_API_KEY
- Go to Google Cloud Console
- Create a new project (or select existing)
- Go to "Credentials" → "Create Credentials" → "OAuth 2.0 Client ID"
- Application type: "Web application"
- Add Authorized redirect URIs:
http://localhost:3000/api/auth/callback/google(development)https://YOUR_CLIENT_DOMAIN/api/auth/callback/google(production)
- Copy:
- Client ID →
GOOGLE_CLIENT_ID - Client Secret →
GOOGLE_CLIENT_SECRET
- Client ID →
- Go to GitHub Developer Settings
- "New OAuth App"
- Fill in:
- Application name:
Apex CLI - Homepage URL:
https://YOUR_CLIENT_DOMAIN - Authorization callback URL:
https://YOUR_CLIENT_DOMAIN/api/auth/callback/github
- Application name:
- Copy:
- Client ID →
GITHUB_CLIENT_ID - Client Secret →
GITHUB_CLIENT_SECRET
- Client ID →
- Go to neon.tech and sign up
- Create a new project
- Copy the connection string:
postgresql://USER:PASSWORD@HOST/DATABASE?sslmode=require - Set as
DATABASE_URL
- Go to supabase.com
- Create new project
- Go to Settings → Database → Connection string
- Copy the URI (with your password)
- Go to railway.app
- New Project → Provision PostgreSQL
- Click on the database → Connect → Copy connection string
cd server
npx prisma generate
npx prisma db pushCreate production .env file:
# Database
DATABASE_URL="postgresql://USER:PASSWORD@HOST/DATABASE?sslmode=require"
# Better Auth
BETTER_AUTH_SECRET="generate-a-random-32-char-string"
BETTER_AUTH_URL="https://YOUR_SERVER_DOMAIN"
# OAuth Providers
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"
# AI
GOOGLE_API_KEY="your-gemini-api-key"
# Frontend URL (for CORS and redirects)
FRONTEND_URL="https://YOUR_CLIENT_DOMAIN"
# Port
PORT=5000-
Install Railway CLI:
npm install -g @railway/cli railway login
-
Create Project:
cd server railway init -
Add PostgreSQL:
railway add --plugin postgresql
-
Set Environment Variables:
railway variables set GOOGLE_CLIENT_ID="..." railway variables set GOOGLE_CLIENT_SECRET="..." railway variables set GITHUB_CLIENT_ID="..." railway variables set GITHUB_CLIENT_SECRET="..." railway variables set GOOGLE_API_KEY="..." railway variables set FRONTEND_URL="https://your-client.vercel.app" railway variables set BETTER_AUTH_SECRET="your-secret"
-
Deploy:
railway up
-
Get Domain:
railway domain
Note this URL as
YOUR_SERVER_DOMAIN
- Go to render.com
- New → Web Service
- Connect your GitHub repo
- Settings:
- Root Directory:
server - Build Command:
npm install && npx prisma generate - Start Command:
npm start
- Root Directory:
- Add Environment Variables (same as above)
- Deploy
Create .env.production:
NEXT_PUBLIC_API_URL="https://YOUR_SERVER_DOMAIN"-
Install Vercel CLI:
npm install -g vercel vercel login
-
Deploy:
cd client vercel -
Set Environment Variables in Vercel Dashboard:
- Go to Project Settings → Environment Variables
- Add:
NEXT_PUBLIC_API_URL=https://YOUR_SERVER_DOMAIN
-
Redeploy:
vercel --prod
-
Note your Vercel URL as
YOUR_CLIENT_DOMAIN
After getting your production URLs, update:
-
Google Cloud Console:
- Add
https://YOUR_CLIENT_DOMAIN/api/auth/callback/google
- Add
-
GitHub OAuth App:
- Update callback URL to
https://YOUR_CLIENT_DOMAIN/api/auth/callback/github
- Update callback URL to
-
Server Environment:
- Update
FRONTEND_URLtohttps://YOUR_CLIENT_DOMAIN
- Update
Update server/package.json:
{
"name": "apex-ai-cli",
"version": "1.0.0",
"description": "AI-powered CLI tool with authentication",
"main": "src/index.js",
"bin": {
"apex": "src/cli/main.js"
},
"files": [
"src/cli/**/*",
"src/lib/**/*",
"src/config/**/*"
],
"keywords": [
"cli",
"ai",
"gemini",
"chat",
"agent"
],
"author": "Your Name",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/YOUR_USERNAME/apex-cli"
},
"engines": {
"node": ">=18.0.0"
}
}Create server/.npmignore:
# Don't publish these
node_modules/
.env
.env.*
prisma/
src/index.js
*.log
.git/
Update src/cli/main.js to use production server:
// Change DEMO_URL in login.js
const DEMO_URL = process.env.APEX_SERVER_URL || "https://YOUR_SERVER_DOMAIN";-
Create NPM Account: Go to npmjs.com and sign up
-
Login:
npm login
-
Test Locally First:
cd server npm link apex --version -
Publish:
npm publish
-
For Updates:
npm version patch # or minor, major npm publish
Create this as a public README for users:
npm install -g apex-ai-cli-
Login to Apex:
apex login
This opens a browser for authentication.
-
Verify Login:
apex whoami
# Interactive AI chat
apex chat
# Chat with tools (file, code, shell access)
apex tools
# Autonomous AI agent
apex agent -t "Create a React todo app"
# Logout
apex logoutCreate ~/.apex-cli/.env for custom settings:
APEX_SERVER_URL=https://your-custom-server.com
GOOGLE_API_KEY=your-own-api-key| Issue | Solution |
|---|---|
GOOGLE_API_KEY not set |
Add GOOGLE_API_KEY to server .env |
| OAuth callback error | Check redirect URIs match exactly |
| Database connection failed | Verify DATABASE_URL and SSL settings |
| CORS errors | Ensure FRONTEND_URL is correct in server |
apex command not found |
Run npm link in server directory |
# Check if CLI is installed
which apex
# Check Node version
node --version
# Check npm global packages
npm list -g --depth=0
# Test server connection
curl https://YOUR_SERVER_DOMAIN/api/health- Railway:
railway logs - Render: Dashboard → Logs
- Vercel: Dashboard → Deployments → Logs
- PostgreSQL database created
- Google OAuth app configured
- GitHub OAuth app configured
- Google Gemini API key obtained
- Server deployed and running
- Client deployed and running
- OAuth redirect URIs updated for production
- Server
FRONTEND_URLpoints to client - Client
NEXT_PUBLIC_API_URLpoints to server - NPM package published
- Test full flow:
apex login→apex chat
Your Apex CLI is now deployed and available for others to install via:
npm install -g apex-ai-cli
apex login
apex chat