JavaScript SDK
Overview​
With the "openapi-generator-cli" Python package and the OpenAPI specification file for the h2oGPTe REST API, we generated SDKs in multiple programming languages, enabling quick integration with the API. One of the SDKs generated is the JavaScript SDK.
- Download JavaScript SDK
Steps to generate and test the JavaScript SDK​
-
Create a folder with the following name:
javascript-sdk
-
Download the OpenAPI specification file of the h2oGPTe REST API and move it to the "javascript-sdk" folder: Download api-spec.yaml.
-
Inside the "javascript-sdk" folder, set up a Python environment with Python 3.8 or later and install the OpenAPI Generator CLI v7.10.0:
python3 -m venv venv
source venv/bin/activate
pip install openapi-generator-cli==7.10.0 -
In the "javascript-sdk" directory, run the following command (CLI) to create the JavaScript SDK using the OpenAPI Generator CLI:
openapi-generator-cli generate \
-i rest_api_spec_h2ogpte.yaml \
-g javascript \
-o sdk \
--additional-properties=packageName=h2ogpte-rest-client,packageVersion=1.6.2,projectName=h2ogpte-rest-client,npmName=h2ogpte-rest-client,npmVersion=h2ogpte-rest-clientnoteThe CLI creates a folder named "sdk" containing the JavaScript SDK.
-
To use the library locally without publishing to a remote npm registry, first install the dependencies:
cd sdk
npm install -
Now, link the library globally in npm:
npm link
-
In the "sdk" directory, create a Node.js project to test the JavaScript SDK:
cd ..
mkdir my-javascript-sdk-project -
In the "my-javascript-sdk-project" directory, initiate a new Node.js project:
cd my-javascript-sdk-project
npm init -y -
To use the link you just defined in the "sdk" folder, navigate to the directory where you want to use your
h2ogpte-rest-client
. For this example, we will run it in themy-javascript-sdk-project
directory:npm link /path/to/<THE_SDK_FOLDER>
-
Now, build the module in the "sdk" directory:
cd ..
cd sdk
npm run build -
In the "my-javascript-sdk-project" directory, create the following file to test the newly created JavaScript SDK (in particular, let's create a Collection to test the SDK):
create-a-collection.mjs// This example with the JavaScript SDK creates a new Collection with the following
// name and description (while testing whether the Python SDK was properly installed):
// Name = The name of my Collection
// Description = The description of my Collection
import H2ogpteRestClient from 'h2ogpte-rest-client';
let defaultClient = H2ogpteRestClient.ApiClient.instance;
let bearerAuth = defaultClient.authentications['bearerAuth'];
// The access token should be a global API key.
bearerAuth.accessToken = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
let apiInstance = new H2ogpteRestClient.CollectionsApi();
let collectionCreateRequest = new H2ogpteRestClient.CollectionCreateRequest(
"The name of my Collection", // name parameter
"The description of my Collection" // description parameter
);
apiInstance.createCollection(collectionCreateRequest, (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
});Access token- All URIs are relative to https://h2ogpte.genai.h2o.ai/. The global API key must be created on this platform to ensure successful requests.
- To learn more about what is a global API key and how to create one, see APIs.
-
In the "my-javascript-sdk-project" directory, run the JavaScript file:
cd ..
cd my-javascript-sdk-project
node create-a-collection.mjsAPI called successfully. Returned data: [object Object]
- Submit and view feedback for this page
- Send feedback about Enterprise h2oGPTe to cloud-feedback@h2o.ai