@usegranthq/sdk v1.0.3
UseGrant SDK
TypeSafe TypeScript SDK for accessing the UseGrant REST API.
Installation
SDK can be installed using npm, bun or pnpm package managers:
npm install @usegranthq/sdk
# or
bun install @usegranthq/sdk
# or
pnpm install @usegranthq/sdk
Authentication
To use the SDK, you need to have an API key. You can create one in the UseGrant Settings Page page. If you face any 401 or 403 errors when you are sending a token, it can be one of the following reasons:
- The token is invalid, check if you have copied the token correctly. Also make sure the token is in format
usegrant_api_<token>
. - The token has expired, you can create a new token and replace the existing token.
Runtime
The SDK uses the Fetch API under the hood, which is supported widely in all modern browsers and Node.js(18+).
Usage
The following example shows how to create a provider using the SDK.
import UseGrant from '@usegrant/sdk';
// Initialize the SDK
const usegrant = new UseGrant('YOUR_API_KEY');
// Create a provider
const provider = await usegrant.createProvider({
name: 'My Provider',
description: 'My Provider Description',
});
Available methods:
createProvider
getProvider
deleteProvider
createClient
getClient
deleteClient
createToken
createTenant
getTenant
deleteTenant
createTenantProvider
getTenantProvider
deleteTenantProvider
validateToken
Refer to the API Reference for more information about the available methods and their parameters.
TypeScript Support
The SDK is written in TypeScript and provides full type safety out of the box. All methods and their parameters are fully typed:
Configuration
Retry Options
The SDK uses the ky library under the hood, which supports retry options. You can pass a retry
option to the constructor to customize the retry behavior.
const usegrant = new UseGrant('YOUR_API_KEY', {
retry: {
limit: 3,
backoffLimit: 1000,
},
});
Refer to the ky retry options for more information about the available options.
Error Handling
The SDK throws a custom error UseGrantError
when you face any errors from the API. You can catch the error and handle it accordingly.
import { UseGrant, UseGrantError } from '@usegrant/sdk';
const usegrant = new UseGrant('YOUR_API_KEY');
try {
const provider = await usegrant.createProvider({
name: 'My Provider',
description: 'My Provider Description',
});
} catch (error) {
if (error instanceof UseGrantError) {
// handle the api error
}
}
Contributing
We welcome contributions! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Run changeset
npx @changeset/cli
to generate the changelog and commit the generated changelog file along with the changes - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to update tests as appropriate and follow the existing coding style.
For reference to changeset, please refer to the Changeset Documentation.