Skip to content

Commit 85ad3d0

Browse files
awalker4Coniferish
andauthored
chore/Document breaking changes in js client (#73)
Co-authored-by: John <[email protected]>
1 parent ea90a2c commit 85ad3d0

File tree

1 file changed

+89
-57
lines changed

1 file changed

+89
-57
lines changed

api-reference/api-services/sdk.mdx

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
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.
44
---
55

66
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)
88
SDK clients allow for easy interaction with the Unstructured API. Whether you're using the
99
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.
1111

1212
## Installation
1313

@@ -28,15 +28,28 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
2828
</Note>
2929

3030
## Basics
31+
Let's start with a simple example in which you send a pdf document to be partitioned via the free Unstructured API:
3132

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>
3342
Python SDK Deprecation Warning (>v0.22.0): The legacy method of passing `shared.PartitionParameters`
3443
directly to `client.general.partition()` is currently supported but may be deprecated and
3544
could break in future releases. Users should migrate to the new `shared.PartitionRequest` object
3645
to ensure compatibility with future updates.
37-
</Note>
46+
</Warning>
3847

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>
4053

4154
<CodeGroup>
4255
```python Python
@@ -45,23 +58,23 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
4558

4659
client = unstructured_client.UnstructuredClient(
4760
api_key_auth="YOUR_API_KEY",
48-
# You only need to provide your unique API URL if using the SaaS Unstructured API
4961
# server_url="YOUR_API_URL",
5062
)
5163

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+
5468
req = operations.PartitionRequest(
5569
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,
5972
file_name=filename,
6073
),
6174
# --- Other partition parameters ---
6275
# Note: Defining `strategy`, `chunking_strategy`, and `output_format`
6376
# 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.
6578
strategy=shared.Strategy.AUTO,
6679
languages=['eng'],
6780
),
@@ -73,52 +86,63 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
7386
# Handle the response
7487
pass
7588
```
76-
```javascript JavaScript
77-
import { openAsBlob } from "node:fs";
89+
```javascript TypeScript
7890
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";
8096

8197
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
8498
// serverURL: "YOUR_API_URL",
99+
security: {
100+
apiKeyAuth: key,
101+
},
85102
});
86103

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);
101106

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+
});
103128
```
104-
```python Python (SDK <v0.22.0)
129+
```python Python (SDK <=v0.22.0)
105130
import unstructured_client
106131
from unstructured_client.models import shared
107132
from unstructured_client.models.errors import SDKError
108133

109134
client = unstructured_client.UnstructuredClient(
110135
api_key_auth="YOUR_API_KEY",
111-
# You only need to provide your unique API URL if using the SaaS Unstructured API
112136
# server_url="YOUR_API_URL",
113137
)
114138

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()
117142

118-
# Requests not wrapped in `shared.PartitionRequest` may be deprecated and break in future versions
119143
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,
122146
file_name=filename,
123147
),
124148
# Other partition parameters
@@ -132,38 +156,46 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
132156
except SDKError as e:
133157
print(e)
134158
```
135-
```javascript JavaScript (SDK <v0.10.6)
159+
```javascript TypeScript (SDK <=v0.10.6)
136160
import { UnstructuredClient } from "unstructured-client";
137161
import { PartitionResponse } from "unstructured-client/dist/sdk/models/operations";
138162
import * as fs from "fs";
139163

140164
const key = "YOUR_API_KEY";
141165

142166
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
145167
// serverURL: "YOUR_API_URL",
168+
security: {
169+
apiKeyAuth: key,
170+
},
146171
});
147172

148-
const filename = "sample-docs/layout-parser-paper.pdf";
173+
const filename = "PATH_TO_FILE";
149174
const data = fs.readFileSync(filename);
150175

151176
client.general.partition({
152-
// The sdk currently only supports sending a single file
153177
files: {
154178
content: data,
155179
fileName: filename,
156180
},
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+
});
161195
```
162196
</CodeGroup>
163197

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.
167199

168200
## SaaS Unstructured API
169201

@@ -178,7 +210,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
178210
server_url="YOUR_API_URL",
179211
)
180212
```
181-
```javascript JavaScript
213+
```javascript TypeScript
182214
const client = new UnstructuredClient({
183215
security: { apiKeyAuth: "YOUR_API_KEY" },
184216
serverURL: "YOUR_API_URL",
@@ -217,7 +249,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
217249
)
218250
res = client.general.partition(req)
219251
```
220-
```javascript JavaScript
252+
```javascript TypeScript
221253
client.general.partition({
222254
partitionParameters: {
223255
files: {
@@ -258,7 +290,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
258290
)
259291
)
260292
```
261-
```javascript JavaScript
293+
```javascript TypeScript
262294
const client = new UnstructuredClient({
263295
security: { apiKeyAuth: "YOUR_API_KEY" },
264296
serverURL: "YOUR_API_URL",
@@ -315,7 +347,7 @@ deployment of Unstructured API, you can access the API using the Python or JavaS
315347
## Parameters & examples
316348

317349
#### 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
319351
convention (while the Python SDK uses snake_case). Other than this difference in naming convention,
320352
the names used in the SDKs are the same across all methods.
321353

0 commit comments

Comments
 (0)