45
45
5 . [ Examples] ( #-examples )
46
46
47
47
## 🛠 Getting started
48
+
48
49
### Pre-Requisites
50
+
49
51
Before you begin, ensure you have the following:
52
+
50
53
- A Strapi backend up and running: [ quick start guide] ( https://docs.strapi.io/dev-docs/quick-start ) .
51
54
- The API URL of your Strapi instance: for example, ` http://localhost:1337/api ` .
52
55
- A recent version of [ Node.js] ( https://nodejs.org/en/download/package-manager ) installed.
53
56
54
57
### Installation
58
+
55
59
Install the SDK as a dependency in your project:
56
60
57
61
** NPM**
62
+
58
63
``` bash
59
64
npm install @strapi/sdk-js
60
65
```
61
66
62
67
** Yarn**
68
+
63
69
``` bash
64
70
yarn add @strapi/sdk-js
65
71
```
66
72
67
73
** pnpm**
74
+
68
75
``` bash
69
76
pnpm add @strapi/sdk-js
70
77
```
71
78
72
79
## ⚙️ Creating and configuring the SDK Instance
80
+
73
81
### Basic configuration
74
82
75
83
To interact with your Strapi backend, initialize the SDK with your Strapi API base URL:
76
84
77
- ``` typescript
85
+ ``` typescript
78
86
import { createStrapiSDK } from ' @strapi/sdk-js' ;
79
87
80
88
const sdk = createStrapiSDK ({ baseURL: ' http://localhost:1337/api' });
81
89
```
82
90
83
91
Alternatively, use a ` <script> ` tag in a browser environment:
84
92
85
- ``` html
93
+ ``` html
86
94
<script src =" https://cdn.jsdelivr.net/npm/@strapi/sdk-js" ></script >
87
95
88
96
<script >
@@ -91,13 +99,14 @@ Alternatively, use a `<script>` tag in a browser environment:
91
99
```
92
100
93
101
### Authentication
102
+
94
103
The SDK supports multiple authentication strategies for accessing authenticated content in your Strapi backend.
95
104
96
105
#### API-Token authentication
97
106
98
107
If your Strapi instance uses API tokens, configure the SDK like this:
99
108
100
- ``` typescript
109
+ ``` typescript
101
110
const sdk = createStrapiSDK ({
102
111
baseURL: ' http://localhost:1337/api' ,
103
112
auth: {
@@ -110,12 +119,14 @@ const sdk = createStrapiSDK({
110
119
## 📚 API Reference
111
120
112
121
The Strapi SDK instance provides key properties and utility methods for content and API interaction:
122
+
113
123
- ** ` baseURL ` ** : base URL of your Strapi backend.
114
124
- ** ` fetch ` ** : perform generic requests to the Strapi Content API using fetch-like syntax.
115
125
- ** ` .collection(resource: string) ` ** : get a manager instance for handling collection-type resources.
116
126
- ** ` .single(resource: string) ` ** : get a manager instance for handling single-type resources.
117
127
118
128
## 📁 Resource Managers
129
+
119
130
### ` .collection(resource) `
120
131
121
132
The ` .collection() ` method provides a manager for working with collection-type resources,
@@ -133,7 +144,7 @@ which can have multiple entries.
133
144
134
145
#### Examples:
135
146
136
- ``` typescript
147
+ ``` typescript
137
148
const articles = sdk .collection (' articles' );
138
149
139
150
// Fetch all english articles sorted by title
@@ -154,6 +165,7 @@ const updatedArticle = await articles.update('article-document-id', { title: 'Up
154
165
// Delete an article
155
166
await articles .delete (' article-id' );
156
167
```
168
+
157
169
### ` .single(resource) `
158
170
159
171
The ` .single() ` method provides a manager for working with collection-type resources, which have only one entry.
@@ -167,7 +179,8 @@ The `.single()` method provides a manager for working with collection-type resou
167
179
3 . ** ` delete(queryParams?) ` ** : remove the document.
168
180
169
181
#### Examples:
170
- ``` typescript
182
+
183
+ ``` typescript
171
184
const homepage = sdk .single (' homepage' );
172
185
173
186
// Fetch the default version of the homepage
@@ -177,7 +190,10 @@ const homepageContent = await homepage.find();
177
190
const homepageContent = await homepage .find ({ locale: ' es' });
178
191
179
192
// Update the homepage draft content
180
- const updatedHomepage = await homepage .update ({ title: ' Updated Homepage Title' }, { status: ' draft' });
193
+ const updatedHomepage = await homepage .update (
194
+ { title: ' Updated Homepage Title' },
195
+ { status: ' draft' }
196
+ );
181
197
182
198
// Delete the homepage content
183
199
await homepage .delete ();
@@ -187,7 +203,7 @@ await homepage.delete();
187
203
188
204
Here’s how to combine ` .collection() ` and ` .single() ` methods in a real-world scenario:
189
205
190
- ``` typescript
206
+ ``` typescript
191
207
const sdk = createStrapiSDK ({
192
208
baseURL: ' http://localhost:1337/api' ,
193
209
auth: {
0 commit comments