1
1
---
2
- title : Python and JavaScript SDKs
3
- description : This documentation covers the usage of the Python and JavaScript SDKs for interacting with the Unstructured API.
2
+ title : Python and TypeScript SDKs
3
+ description : This documentation covers the usage of the Python and TypeScript SDKs for interacting with the Unstructured API.
4
4
---
5
5
6
6
The [ Python] ( https://github.com/Unstructured-IO/unstructured-python-client ) and
7
- [ JavaScript ] ( https://github.com/Unstructured-IO/unstructured-js-client )
7
+ [ TypeScript ] ( https://github.com/Unstructured-IO/unstructured-js-client )
8
8
SDK clients allow for easy interaction with the Unstructured API. Whether you're using the
9
9
free Unstructured API, the SaaS Unstructured API, Unstructured API on Azure/AWS or your local
10
- deployment of Unstructured API, you can access the API using the Python or JavaScript SDK.
10
+ deployment of Unstructured API, you can access the API using the Python or TypeScript SDK.
11
11
12
12
## Installation
13
13
@@ -28,15 +28,28 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
28
28
</Note >
29
29
30
30
## Basics
31
+ Let's start with a simple example in which you send a pdf document to be partitioned via the free Unstructured API:
31
32
32
- <Note >
33
+ <Warning >
34
+ The TypeScript SDK has the following breaking changes in v0.11.0:
35
+ * Imports under the ` dist ` path have moved up a level
36
+ * Enums are now used for parameters with a set of options
37
+ * This includes ` chunkingStrategy ` , ` outputFormat ` , and ` strategy `
38
+ * All parameters to ` partition ` have moved to a ` partitionParameters ` object
39
+ </Warning >
40
+
41
+ <Warning >
33
42
Python SDK Deprecation Warning (>v0.22.0): The legacy method of passing ` shared.PartitionParameters `
34
43
directly to ` client.general.partition() ` is currently supported but may be deprecated and
35
44
could break in future releases. Users should migrate to the new ` shared.PartitionRequest ` object
36
45
to ensure compatibility with future updates.
37
- </Note >
46
+ </Warning >
38
47
39
- Let's start with a simple example in which you send a pdf document to be partitioned via the free Unstructured API:
48
+ <Note >
49
+ By default, the SDKs send requests to the free API. In order to access the paid API, you'll need
50
+ to include the argument for the server URL. If you receive authentication errors, and you've
51
+ checked that your key is correct, be sure that you are accessing the correct endpoint.
52
+ </Note >
40
53
41
54
<CodeGroup >
42
55
``` python Python
@@ -45,23 +58,23 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
45
58
46
59
client = unstructured_client.UnstructuredClient(
47
60
api_key_auth = " YOUR_API_KEY" ,
48
- # You only need to provide your unique API URL if using the SaaS Unstructured API
49
61
# server_url="YOUR_API_URL",
50
62
)
51
63
52
- filename = " sample-docs/layout-parser-paper.pdf"
53
- file = open (filename, " rb" )
64
+ filename = " PATH_TO_FILE"
65
+ with open (filename, " rb" ) as f:
66
+ data = f.read()
67
+
54
68
req = operations.PartitionRequest(
55
69
partition_parameters = shared.PartitionParameters(
56
- # --- `files` is the only required parameter ---
57
- files = shared.Files( # The sdk currently only supports sending a single file
58
- content = file .read(),
70
+ files = shared.Files(
71
+ content = data,
59
72
file_name = filename,
60
73
),
61
74
# --- Other partition parameters ---
62
75
# Note: Defining `strategy`, `chunking_strategy`, and `output_format`
63
76
# parameters as strings is accepted, but will not pass strict type checking. It is
64
- # advised to use the new enum classes for defining those parameters as shown below.
77
+ # advised to use the defined enum classes as shown below.
65
78
strategy = shared.Strategy.AUTO ,
66
79
languages = [' eng' ],
67
80
),
@@ -73,52 +86,63 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
73
86
# Handle the response
74
87
pass
75
88
```
76
- ``` javascript JavaScript
77
- import { openAsBlob } from " node:fs" ;
89
+ ``` javascript TypeScript
78
90
import { UnstructuredClient } from " unstructured-client" ;
79
- import { Strategy , ChunkingStrategy } from " unstructured-client/sdk/models/shared" ;
91
+ import { PartitionResponse } from " unstructured-client/sdk/models/operations" ;
92
+ import { Strategy } from " unstructured-client/sdk/models/shared" ;
93
+ import * as fs from " fs" ;
94
+
95
+ const key = " YOUR_API_KEY" ;
80
96
81
97
const client = new UnstructuredClient ({
82
- security: { apiKeyAuth: " YOUR_API_KEY" },
83
- // You only need to provide your unique API URL if using the SaaS Unstructured API
84
98
// serverURL: "YOUR_API_URL",
99
+ security: {
100
+ apiKeyAuth: key,
101
+ },
85
102
});
86
103
87
- async function run () {
88
- const res = await client .general .partition ({
89
- partitionParameters: {
90
- // --- `files` is the only required parameter ---
91
- files: await openAsBlob (" sample-docs/layout-parser-paper.pdf" ), // The sdk currently only supports sending a single file
92
- // --- Other partition params ---
93
- strategy: Strategy .Auto ,
94
- languages: [' eng' ],
95
- },
96
- });
97
-
98
- // Handle the response
99
- console .log (res)
100
- }
104
+ const filename = " PATH_TO_FILE" ;
105
+ const data = fs .readFileSync (filename);
101
106
102
- run ();
107
+ client .general .partition ({
108
+ partitionParameters: {
109
+ files: {
110
+ content: data,
111
+ fileName: filename,
112
+ },
113
+ strategy: Strategy .Auto ,
114
+ languages: [' eng' ],
115
+ }
116
+ }).then ((res : PartitionResponse ) => {
117
+ if (res .statusCode == 200 ) {
118
+ console .log (res .elements );
119
+ }
120
+ }).catch ((e ) => {
121
+ if (e .statusCode ) {
122
+ console .log (e .statusCode );
123
+ console .log (e .body );
124
+ } else {
125
+ console .log (e);
126
+ }
127
+ });
103
128
```
104
- ``` python Python (SDK <v0.22.0)
129
+ ``` python Python (SDK <= v0.22.0)
105
130
import unstructured_client
106
131
from unstructured_client.models import shared
107
132
from unstructured_client.models.errors import SDKError
108
133
109
134
client = unstructured_client.UnstructuredClient(
110
135
api_key_auth = " YOUR_API_KEY" ,
111
- # You only need to provide your unique API URL if using the SaaS Unstructured API
112
136
# server_url="YOUR_API_URL",
113
137
)
114
138
115
- filename = " sample-docs/layout-parser-paper.pdf"
116
- file = open (filename, " rb" )
139
+ filename = " PATH_TO_FILE"
140
+ with open (filename, " rb" ) as f:
141
+ data = f.read()
117
142
118
- # Requests not wrapped in `shared.PartitionRequest` may be deprecated and break in future versions
119
143
req = shared.PartitionParameters(
120
- files = shared.Files( # The sdk currently only supports sending a single file
121
- content = file .read() ,
144
+ files = shared.Files(
145
+ content = data ,
122
146
file_name = filename,
123
147
),
124
148
# Other partition parameters
@@ -132,38 +156,46 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
132
156
except SDKError as e:
133
157
print (e)
134
158
```
135
- ``` javascript JavaScript (SDK <v0.10.6)
159
+ ``` javascript TypeScript (SDK <= v0.10.6)
136
160
import { UnstructuredClient } from " unstructured-client" ;
137
161
import { PartitionResponse } from " unstructured-client/dist/sdk/models/operations" ;
138
162
import * as fs from " fs" ;
139
163
140
164
const key = " YOUR_API_KEY" ;
141
165
142
166
const client = new UnstructuredClient ({
143
- security: { apiKeyAuth: " YOUR_API_KEY" },
144
- // You only need to provide your unique API URL if using the SaaS Unstructured API
145
167
// serverURL: "YOUR_API_URL",
168
+ security: {
169
+ apiKeyAuth: key,
170
+ },
146
171
});
147
172
148
- const filename = " sample-docs/layout-parser-paper.pdf " ;
173
+ const filename = " PATH_TO_FILE " ;
149
174
const data = fs .readFileSync (filename);
150
175
151
176
client .general .partition ({
152
- // The sdk currently only supports sending a single file
153
177
files: {
154
178
content: data,
155
179
fileName: filename,
156
180
},
157
- // Other partition params
158
- strategy: " auto" ,
159
- languages: [' eng' ],
160
- })
181
+ strategy: ' auto' ,
182
+ languages: [' eng' ]
183
+ }).then ((res : PartitionResponse ) => {
184
+ if (res .statusCode == 200 ) {
185
+ console .log (res .elements );
186
+ }
187
+ }).catch ((e ) => {
188
+ if (e .statusCode ) {
189
+ console .log (e .statusCode );
190
+ console .log (e .body );
191
+ } else {
192
+ console .log (e);
193
+ }
194
+ });
161
195
```
162
196
</CodeGroup >
163
197
164
- In the example above we're sending the request to the free Unstructured API, where the API URL is
165
- the same for all users, so you don't need to provide the server URL. However, you still need to
166
- authenticate using your individual API Key.
198
+ Note that currently, the SDKs only support sending one file at a time.
167
199
168
200
## SaaS Unstructured API
169
201
@@ -178,7 +210,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
178
210
server_url = " YOUR_API_URL" ,
179
211
)
180
212
```
181
- ``` javascript JavaScript
213
+ ``` javascript TypeScript
182
214
const client = new UnstructuredClient ({
183
215
security: { apiKeyAuth: " YOUR_API_KEY" },
184
216
serverURL: " YOUR_API_URL" ,
@@ -217,7 +249,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
217
249
)
218
250
res = client.general.partition(req)
219
251
```
220
- ``` javascript JavaScript
252
+ ``` javascript TypeScript
221
253
client .general .partition ({
222
254
partitionParameters: {
223
255
files: {
@@ -258,7 +290,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
258
290
)
259
291
)
260
292
```
261
- ``` javascript JavaScript
293
+ ``` javascript TypeScript
262
294
const client = new UnstructuredClient ({
263
295
security: { apiKeyAuth: " YOUR_API_KEY" },
264
296
serverURL: " YOUR_API_URL" ,
@@ -315,7 +347,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
315
347
## Parameters & examples
316
348
317
349
#### Parameter names
318
- The parameter names used in this document are for the JavaScript SDK, which follow camelCase
350
+ The parameter names used in this document are for the TypeScript SDK, which follow camelCase
319
351
convention (while the Python SDK uses snake_case). Other than this difference in naming convention,
320
352
the names used in the SDKs are the same across all methods.
321
353
0 commit comments