You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 20, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/guides/dart/serverless-rest-api-example.mdx
+65-85Lines changed: 65 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@ tags:
7
7
languages:
8
8
- dart
9
9
published_at: 2024-04-24
10
+
updated_at: 2025-01-06
10
11
---
11
12
12
13
# 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
40
41
41
42
## Getting started
42
43
43
-
Start by creating a base Dart
44
+
Start by creating a new Nitric project from the dart template.
44
45
45
46
```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
55
48
```
56
49
57
50
Next, open the project in your editor of choice.
@@ -63,62 +56,29 @@ cd my-profile-api
63
56
The scaffolded project should have the following structure:
64
57
65
58
```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
72
61
.gitignore
73
62
analysis_options.yaml
74
-
CHANGELOG.md
75
-
pubspec.lock
63
+
dart.dockerfile
64
+
dart.dockerfile.dockerignore
65
+
nitric.yaml
76
66
pubspec.yaml
77
67
README.md
78
68
```
79
69
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`.
112
71
72
+
```bash
73
+
dart pub add uuid
113
74
```
114
75
115
76
## Building the API
116
77
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.
118
79
119
-
```dart
80
+
```dart title:services/api.dart
120
81
import 'package:nitric_sdk/nitric.dart';
121
-
import 'package:nitric_sdk/resources.dart';
122
82
123
83
import 'package:uuid/uuid.dart';
124
84
@@ -128,22 +88,22 @@ void main() {
128
88
129
89
// Define a key value store named 'profiles', then request get, set and delete permissions.
130
90
final profiles = Nitric.kv("profiles").allow([
131
-
KeyValuePermission.get,
132
-
KeyValuePermission.set,
133
-
KeyValuePermission.delete
91
+
KeyValueStorePermission.get,
92
+
KeyValueStorePermission.set,
93
+
KeyValueStorePermission.delete
134
94
]);
135
95
}
136
96
```
137
97
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.
139
99
140
100
<Note>
141
101
Resources in Nitric like `api` and `key value store` represent high-level
142
102
cloud resources. When your app is deployed Nitric automatically converts these
143
103
requests into appropriate resources for the specific
144
104
[provider](https://nitric.io/docs/reference/providers). Nitric also takes care
145
105
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
147
107
Google Cloud.
148
108
</Note>
149
109
@@ -156,13 +116,13 @@ Let's start adding features that allow our API consumers to work with profile da
156
116
prefer. For simplicity we'll group them together in this guide.
@@ -221,7 +200,7 @@ Now that you have an API defined with handlers for each of its methods, it's tim
221
200
nitric start
222
201
```
223
202
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.
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:
263
242
264
243
-[AWS](/providers/pulumi/aws)
265
244
-[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.
267
248
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.
269
250
270
251
```bash
271
-
nitric stack new
252
+
nitric stack new dev aws
272
253
```
273
254
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`.
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.
298
278
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:
300
280
301
281
```bash
302
282
nitric down
@@ -310,13 +290,13 @@ If you want to go a bit deeper and create some other resources with Nitric, why
310
290
311
291
Define a bucket named `profilesImg` with reading/writing permissions.
312
292
313
-
```dart
293
+
```dart title:services/api.dart
314
294
final profilesImg = Nitric.bucket("profilesImg").allow([BucketPermission.read, BucketPermission.write]);
0 commit comments