Quick start 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. To use the Studio to create projects, check out this quickstart.
You can find the Studio application in your account at account.codehooks.io. You can also create projects and spaces there, which will deploy a default CRUD API. To change and deploy new code, you will have to use the CLI (until this is available in the Studio).
Install the CLIโ
Install the Codehooks command line interface (CLI), this lets you fully manage your projects from the command line.
npm install codehooks -g
You need Node.js to install the CLI.
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
Create projectโ
Lets go forward and 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.
Finally change directory to the new project and install the Codehooks standard library.
cd myproject
The coho create
command has now 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.
Create a serverless JavaScript Codehookโ
First, in the project directory, install the Codehooks standard open source libraries codehooks-js and codehooks-crudlify.
npm install codehooks-js codehooks-crudlify --save
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'
import {crudlify} from 'codehooks-crudlify'
// 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 collection
crudlify(app)
// bind to serverless runtime
export default app.init();
Save the JavaScript file.
You are now ready to deploy your project.
Deploy the code to the serverless cloudโ
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.
๐๐