Getting started

Before you begin, you'll need to install the Nitric CLI.

brew install nitrictech/tap/nitric

For more installation options and dependency information review the full installation guide.

Using the new command

The Nitric CLI's new command provides prompts to scaffold new projects from templates. Here's an example of creating a new project from a template:

nitric new hello-world ts-starter

Navigate to the new project directory and install the dependencies:

cd hello-world
npm install

Your project should now look like this:

+--services/
| +-- hello.ts
+--node_modules/
| ...
+--package-lock.json
+--package.json
+--nitric.yaml
+--README.md

Running your app

Nitric provides a local development server offering emulated versions of cloud services, suitable for local development/testing. You can use the Nitric CLI to start the local server using the nitric start command.

nitric start

Nitric will automatically run your application using the service start script, this is located in your nitric.yaml.

After your service is running, it will register itself with the server. All the APIs are locally hosted on their own port, which will be displayed in the CLI output as http://localhost:<port_number>.

The output will also show the local dashboard URL, the dashboard will automatically refresh with API details and other useful tools for building your application.

Once the API is registered, you can test the API using any HTTP client, your browser, or the local dashboard:

curl http://localhost:4001/hello/John
Hello John

Your local dashboard

Access the local development dashboard by opening the URL and port displayed in the CLI output.

The development dashboard is a powerful tool designed to help you move faster when developing new features. With the API explorer, you can easily access automatically generated endpoints and path params. Additionally, the Schedules Explorer allows you to trigger your schedules with ease, making it easier than ever to stay on top of your tasks. Make the most of your local development process with this essential tool.

Watch this brief video to see the development dashboard in action. You'll get a firsthand look at how the dashboard streamlines your development process by showcasing the debugging of APIs and schedules in the popular code editor, VS Code.

The video currently showcases v0 of the local dashboard and will be updated soon.

Making updates

Nitric's language templates enable hot-reloading by default, so at this point, you can start making changes to services and see what happens.

Start by opening the hello service in your editor and adding a new route to the API, then save, and execute the file:

import { api } from '@nitric/sdk'
const helloApi = api('main')
helloApi.get('/hello/:name', async (ctx) => {
const { name } = ctx.req.params
ctx.res.body = `Hello ${name}`
return ctx
})
// Let's add a 'goodbye' route to the API, like this:
helloApi.get('/goodbye/:name', async (ctx) => {
const { name } = ctx.req.params
ctx.res.body = `Goodbye ${name}`
return ctx
})

Depending on the language you choose the template may support hot-reloading, if not you'll need to restart the service.

After saving the file, the new route will be registered and you can test it:

curl http://localhost:4001/goodbye/John
Goodbye John

When you're finished testing, you can stop your application and the Nitric Server.

Deploying the app

Now that you've implemented a basic API and tested that it works, you can deploy it to one or more cloud platforms. Applications built with Nitric can be automatically deployed and run on multiple cloud providers without any code changes.

The first step is to set up your credentials for the cloud provider.

You'll then need to create a stack that represents your project and a deployment target.

The stack new command below will create a stack named dev that uses the aws provider.

nitric stack new dev aws

Continue by checking your stack file nitric.dev.yaml and adding in your preferred region, such as us-east-1.

Now you can deploy your dev stack with the up command.

nitric up

When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your API.

To tear down the stack use the down command:

nitric down

What's next?

Last updated on Oct 11, 2024