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-worldnpm 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 environment offering emulated versions of cloud services, suitable for local development/testing. You can use the Nitric CLI to start your application and the local environment 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
.
name: exampleservices:- match: services/*.tsstart: npm run dev:services $SERVICE_PATH
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 details about your resources and tools to interact with each of them.
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/JohnHello John
Making updates
At this point, you can start making changes to services. Start by opening the hello
service in your editor and adding a new route to the API:
import { api } from '@nitric/sdk'const helloApi = api('main')helloApi.get('/hello/:name', async (ctx) => {const { name } = ctx.req.paramsctx.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.paramsctx.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 nitric start
command.
After making the change, the new route will be registered and you can test it:
curl http://localhost:4001/goodbye/JohnGoodbye John
When you're finished testing, you can stop the application.
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 nitric 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?
- Learn more about APIs
- Learn more about the foundations of Nitric