Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 30c6f97

Browse files
update guides
1 parent 280cdc1 commit 30c6f97

12 files changed

+285
-191
lines changed

docs/guides/dart/flutter.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,15 +1505,15 @@ web
15051505
windows
15061506
```
15071507

1508-
### AWS
1508+
### Deploy
1509+
1510+
Now that the application has been configured for deployment, let's try deploying it with the `up` command.
15091511

15101512
<Note>
15111513
Cloud deployments incur costs and while most of these resource are available
15121514
with free tier pricing you should consider the costs of the deployment.
15131515
</Note>
15141516

1515-
Now that the application has been configured for deployment, let's try deploying it with the `up` command.
1516-
15171517
```bash
15181518
nitric up
15191519

docs/guides/dart/serverless-rest-api-example.mdx

Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
languages:
88
- dart
99
published_at: 2024-04-24
10+
updated_at: 2025-01-06
1011
---
1112

1213
# Building a REST API with Nitric
@@ -40,18 +41,10 @@ There is also an extended section of the guide that adds file operations using a
4041

4142
## Getting started
4243

43-
Start by creating a base Dart
44+
Start by creating a new Nitric project from the dart template.
4445

4546
```bash
46-
dart create -t console my-profile-api
47-
```
48-
49-
Add the Nitric SDK and the uuid dependency by adding it to your `pubspec.yaml`.
50-
51-
```yaml
52-
dependencies:
53-
nitric_sdk: ^1.2.0
54-
uuid: ^4.3.3
47+
nitric new my-profile-api dart-starter
5548
```
5649

5750
Next, open the project in your editor of choice.
@@ -63,62 +56,29 @@ cd my-profile-api
6356
The scaffolded project should have the following structure:
6457

6558
```text
66-
bin/
67-
├── my_profile_api.dart
68-
lib/
69-
├── my_profile_api.dart
70-
test/
71-
├── my_profile_api_test.dart
59+
services/
60+
├── api.dart
7261
.gitignore
7362
analysis_options.yaml
74-
CHANGELOG.md
75-
pubspec.lock
63+
dart.dockerfile
64+
dart.dockerfile.dockerignore
65+
nitric.yaml
7666
pubspec.yaml
7767
README.md
7868
```
7969

80-
To create our Nitric project, we have to create a `nitric.yaml` file. The handlers key will point to where our
81-
82-
```yaml
83-
name: my_profile_api
84-
services:
85-
- match: bin/my_profile_api.dart
86-
start: dart run $SERVICE_PATH
87-
```
88-
89-
## Create a Profile class
90-
91-
We will create a class to represent the profiles that we will store in the key value store. We will add `toJson` and `fromJson` functions to assist.
92-
93-
```dart
94-
class Profile {
95-
String name;
96-
int age;
97-
String homeTown;
98-
99-
Profile(this.name, this.age, this.homeTown);
100-
101-
Profile.fromJson(Map<String, dynamic> contents)
102-
: name = contents["name"] as String,
103-
age = contents["age"] as int,
104-
homeTown = contents["homeTown"] as String;
105-
106-
Map<String, dynamic> toJson() => {
107-
'name': name,
108-
'age': age,
109-
'homeTown': homeTown,
110-
};
111-
}
70+
As we will be generating IDs for each profile, add the uuid dependency by adding it to your `pubspec.yaml`.
11271

72+
```bash
73+
dart pub add uuid
11374
```
11475

11576
## Building the API
11677

117-
Applications built with Nitric can contain many APIs, let's start by adding one to this project to serve as the public endpoint. Rename `bin/my_profile_api.dart` to `bin/profiles.dart`
78+
Applications built with Nitric can contain many APIs, let's start by adding one to this project to serve as the public endpoint.
11879

119-
```dart
80+
```dart title:services/api.dart
12081
import 'package:nitric_sdk/nitric.dart';
121-
import 'package:nitric_sdk/resources.dart';
12282
12383
import 'package:uuid/uuid.dart';
12484
@@ -128,22 +88,22 @@ void main() {
12888
12989
// Define a key value store named 'profiles', then request get, set and delete permissions.
13090
final profiles = Nitric.kv("profiles").allow([
131-
KeyValuePermission.get,
132-
KeyValuePermission.set,
133-
KeyValuePermission.delete
91+
KeyValueStorePermission.get,
92+
KeyValueStorePermission.set,
93+
KeyValueStorePermission.delete
13494
]);
13595
}
13696
```
13797

138-
Here we're creating an API named `public` and a key value store named `profiles`, then requesting get, set, and delete permissions which allows our function to access the key value store.
98+
Here we're creating an API named `public` and a key value store named `profiles`, then requesting get, set, and delete permissions which allows our service to access the key value store.
13999

140100
<Note>
141101
Resources in Nitric like `api` and `key value store` represent high-level
142102
cloud resources. When your app is deployed Nitric automatically converts these
143103
requests into appropriate resources for the specific
144104
[provider](https://nitric.io/docs/reference/providers). Nitric also takes care
145105
of adding the IAM roles, policies, etc. that grant the requested access. For
146-
example the `key value stores` resource uses DynamoDB in AWS or FireStore on
106+
example the `key value store` resource uses DynamoDB in AWS or FireStore on
147107
Google Cloud.
148108
</Note>
149109

@@ -156,13 +116,13 @@ Let's start adding features that allow our API consumers to work with profile da
156116
prefer. For simplicity we'll group them together in this guide.
157117
</Note>
158118

159-
```dart
119+
```dart title:services/api.dart
160120
profileApi.post("/profiles", (ctx) async {
161121
final uuid = Uuid();
162122
163123
final id = uuid.v4();
164124
165-
final profile = Profile.fromJson(ctx.req.json());
125+
final profile = ctx.req.json();
166126
167127
// Store the new profile in the profiles kv store
168128
await profiles.set(id, profile);
@@ -176,14 +136,14 @@ profileApi.post("/profiles", (ctx) async {
176136

177137
### Retrieve a profile with GET
178138

179-
```dart
139+
```dart title:services/api.dart
180140
profileApi.get("/profiles/:id", (ctx) async {
181141
final id = ctx.req.pathParams["id"]!;
182142
183143
try {
184144
// Retrieve and return the profile data
185145
final profile = await profiles.get(id);
186-
ctx.res.json(profile.toJson());
146+
ctx.res.json(profile);
187147
} on Exception catch (e) {
188148
print(e);
189149
ctx.res.status = 404;
@@ -194,9 +154,28 @@ profileApi.get("/profiles/:id", (ctx) async {
194154
});
195155
```
196156

157+
### Retrieve all profiles with GET
158+
159+
```dart title:services/api.dart
160+
profileApi.get("/profiles", (ctx) async {
161+
List<Map<String, dynamic>> profilesList = [];
162+
final profilesIds = await profiles.keys();
163+
164+
await for (final id in profilesIds) {
165+
final profile = await profiles.get(id);
166+
profilesList.add(profile);
167+
}
168+
169+
ctx.res.body = jsonEncode(profilesList);
170+
ctx.res.headers["Content-Type"] = ["application/json"];
171+
172+
return ctx;
173+
});
174+
```
175+
197176
### Remove a profile with DELETE
198177

199-
```dart
178+
```dart title:services/api.dart
200179
profileApi.delete("/profiles/:id", (ctx) async {
201180
final id = ctx.req.pathParams["id"]!;
202181
@@ -221,7 +200,7 @@ Now that you have an API defined with handlers for each of its methods, it's tim
221200
nitric start
222201
```
223202

224-
Once it starts, the application will receive requests via the API port. You can use the Local Dashboard or any HTTP client to test the API. We'll keep it running for our tests. If you want to update your functions, just save them, they'll be reloaded automatically.
203+
Once it starts, the application will receive requests via the API port. You can use the [Local Dashboard](/get-started/foundations/projects/local-development) or any HTTP client to test the API. We'll keep it running for our tests. If you want to update your functions, just save them, they'll be reloaded automatically.
225204

226205
## Test the API
227206

@@ -259,44 +238,45 @@ curl --location --request DELETE 'http://localhost:4001/profiles/[id]'
259238

260239
## Deploy to the cloud
261240

262-
At this point, you can deploy the application to any supported cloud provider. Start by setting up your credentials and any configuration for the cloud you prefer:
241+
At this point, you can deploy what you've built to any of the supported cloud providers. To do this start by setting up your credentials and any configuration for the cloud you prefer:
263242

264243
- [AWS](/providers/pulumi/aws)
265244
- [Azure](/providers/pulumi/azure)
266-
- [Google Cloud](/providers/pulumi/gcp)
245+
- [GCP](/providers/pulumi/gcp)
246+
247+
Next, we'll need to create a `stack`. A stack represents a deployed instance of an application, which is a key value store of resources defined in your project. You might want separate stacks for each environment, such as stacks for `dev`, `test` and `prod`. For now, let's start by creating a `dev` stack.
267248

268-
Next, we'll need to create a `stack`. Stacks represent deployed instances of an application, including the target provider and other details such as the deployment region. You'll usually define separate stacks for each environment such as development, testing and production. For now, let's start by creating a `dev` stack.
249+
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
269250

270251
```bash
271-
nitric stack new
252+
nitric stack new dev aws
272253
```
273254

274-
```
275-
? What should we name this stack? dev
276-
? Which provider do you want to deploy with? aws
277-
? Which region should the stack deploy to? us-east-1
278-
```
255+
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region, let's use `us-east-1`.
279256

280-
### AWS
257+
```yaml title:nitric.dev.yaml
258+
# The nitric provider to use
259+
provider: nitric/aws@latest
260+
# The target aws region to deploy to
261+
# See available regions:
262+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
263+
region: us-east-1
264+
```
281265
282266
<Note>
283267
Cloud deployments incur costs and while most of these resource are available
284268
with free tier pricing you should consider the costs of the deployment.
285269
</Note>
286270
287-
In the previous step we called our stack `dev`, let's try deploying it with the `up` command.
271+
We called our stack `dev`, let's try deploying it with the `up` command
288272

289273
```bash
290274
nitric up
291-
┌───────────────────────────────────────────────────────────────┐
292-
| API | Endpoint |
293-
| main | https://XXXXXXXX.execute-api.us-east-1.amazonaws.com |
294-
└───────────────────────────────────────────────────────────────┘
295275
```
296276

297-
When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your API. If you'd like to make changes to the API you can apply those changes by rerunning the `up` command. Nitric will automatically detect what's changed and just update the relevant cloud resources.
277+
When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your API.
298278

299-
When you're done testing your application you can tear it down from the cloud, use the `down` command:
279+
To tear down your application from the cloud, use the `down` command:
300280

301281
```bash
302282
nitric down
@@ -310,13 +290,13 @@ If you want to go a bit deeper and create some other resources with Nitric, why
310290

311291
Define a bucket named `profilesImg` with reading/writing permissions.
312292

313-
```dart
293+
```dart title:services/api.dart
314294
final profilesImg = Nitric.bucket("profilesImg").allow([BucketPermission.read, BucketPermission.write]);
315295
```
316296

317297
### Get a URL to upload a profile image
318298

319-
```dart
299+
```dart title:services/api.dart
320300
profileApi.get("/profiles/:id/image/upload", (ctx) async {
321301
final id = ctx.req.pathParams["id"];
322302
@@ -331,7 +311,7 @@ profileApi.get("/profiles/:id/image/upload", (ctx) async {
331311

332312
### Get a URL to download a profile image
333313

334-
```dart
314+
```dart title:services/api.dart
335315
profileApi.get("/profiles/:id/image/download", (ctx) async {
336316
final id = ctx.req.pathParams["id"];
337317
@@ -346,7 +326,7 @@ profileApi.get("/profiles/:id/image/download", (ctx) async {
346326

347327
You can also return a redirect response that takes the HTTP client directly to the photo URL.
348328

349-
```dart
329+
```dart title:services/api.dart
350330
profileApi.get("/profiles/:id/image/view", (ctx) async {
351331
final id = ctx.req.pathParams["id"];
352332

0 commit comments

Comments
 (0)