Quickstart using the CLI
This short tutorial will show you how to create codehooks projects and spaces using the command line interface (CLI). Check out the concepts page for a more in-depth introduction.
If you want to deploy your first project without using the CLI, you can also use the web-based Studio to create projects directly in your browser. All new projects automatically deploy a space with a database CRUD API using app.crudlify().
Install the CLI
Install the Codehooks command line interface (CLI), this lets you fully manage your projects from the command line.
npm i -g codehooks
You need Node.js to install the CLI.
Note: The CLI command is
coho(short for codehooks). You can also use the fullcodehookscommand - both work the same way.
Our CLI requires use of the terminal window, which can sometimes be challenging especially for Windows users. Check out our blog post about how to set up a dev container for simplified and more consistent codehooks.io development environment on all platforms.
Sign up and Log in
Next you need to sign up / log in to your account (it's totally free), in this example we use a Github account. Your browser will open and direct you a secure login page, and there you can return to the CLI - logged in.
coho login
Then choose login method, e.g. Github.
Select option below
❯ Use Github
Use Google
Exit
Deploy a Webhook Handler in 30 Seconds
The fastest way to get started is to use one of our production-ready webhook templates. These templates include everything you need: signature verification, database storage, and proper error handling.
# Deploy a Stripe webhook handler
coho create mystripehandler --template webhook-stripe-minimal
# Deploy a Shopify webhook handler
coho create myshopifyhandler --template webhook-shopify-minimal
# Deploy a GitHub webhook handler
coho create mygithubhandler --template webhook-github-minimal
After running one of these commands, the CLI will create a project folder with the webhook handler code. Change into the project directory and install dependencies:
cd mystripehandler # or your chosen project name
npm install
Now deploy the webhook handler to your space:
coho deploy
That's it! Your webhook endpoint is immediately ready at:
https://<your-project>.api.codehooks.io/dev/webhooks/<service>
Add your webhook secrets (Stripe API key, etc.) via the CLI (coho set-env <secret-name> <secret-value>) or web UI, then configure your webhook URL in Stripe/Shopify/GitHub's dashboard and start receiving events.
You can also install a template into an existing project using:
coho install
The CLI will display a list of all available templates. You can also visit the templates repository for the complete list including Discord, Twilio, Clerk, and Slack webhooks.This is useful when you want to add webhook handlers to a project you've already created.
Continue reading below if you want to create a custom project instead.
Create a Custom Project: Deploy a CRUD API
If you prefer to create a custom project instead of using a webhook template, follow these steps to deploy a CRUD API project.
Create project
Let's create a new project on your account. A project contains the source code for your serverless functions.
coho create myproject
Follow the guide to create a personal or team project type.
Change directory to the new project and install dependencies:
cd myproject
npm install
The coho create command has created a new project space and generated a unique name for the API, in this example it's named myproject-2b39 and dev.
Your account will be the owner of the project. This can be changed later.
If you have an existing project you'd like to use, for example created with the Studio application, you can connect and deploy the code to that project using the CLI command coho init instead of coho create.
Inspect the CRUD API code
Next, start your favorite code editor and open the auto generated index.js in your project directory.
/*
* Auto generated Codehooks (c) example
*/
import { app } from 'codehooks-js';
// test route for https://<PROJECTID>.api.codehooks.io/dev/
app.get('/', (req, res) => {
res.send('CRUD server ready');
});
// Use Crudlify to create a REST API for any database collection
app.crudlify();
// bind to serverless runtime
export default app.init();
app.crudlify enables a complete database CRUD REST API for any collection. POSTing to a route, e.g. /users, will create a new user in the database collection users. GETing to the same route will return all users in the database collection users. Check out the database CRUD REST API for more details.
Save the JavaScript file if you made any changes.
Codehooks supports TypeScript (version 5) with strong typing. Just rename the index.js file to index.ts, update the package.json main property, and you're ready to go.
/* TypeScript */
import { app, httpRequest, httpResponse } from 'codehooks-js';
// strong typing for request and response
app.get('/', (req: httpRequest, res: httpResponse) => {
res.send('CRUD server ready');
});
// Use Crudlify to create a REST API for any database collection
app.crudlify();
// bind to serverless runtime
export default app.init();
{
// rest of your package.json file
"main": "index.ts"
}
Deploy the code
coho deploy
Example output from the deploy command.
Deploying to Project: myproject-2b39 Space: dev
Deployed Codehook successfully! 🙌
You can now test the database CRUD REST API with a sample collection, for example users.
Use the coho info --examples command to find your project name, API tokens and working curl examples.
curl --location 'https://<YOUR-PROJECT-NAME>.api.codehooks.io/dev/users' \
--header 'x-apikey: <YOUR-API-TOKEN-HERE>' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Bill",
"email": "[email protected]",
"active": true
}'
Example output from the curl test.
{
"name": "Bill",
"email": "[email protected]",
"active": true,
"_id": "6421b3e6a3c762051688edf7"
}
Lets also test a query for the same data we've added.
curl --location 'https://<YOUR-PROJECT-NAME>.api.codehooks.io/dev/users?name=Bill' \
--header 'x-apikey: <YOUR-API-TOKEN-HERE>' \
--header 'Content-Type: application/json' \
Which returns an array of 1 object from the database.
[
{
name: 'Bill',
email: '[email protected]',
active: true,
_id: '6421b3e6a3c762051688edf7',
},
];
Our test shows that the automatic REST API is accessible on the /dev/users route, and we can successfully add and retrieve data from the database to the client.
👏👏