Installing an API Client

Gadget autogenerates API client packages specific to Simple Blog Example in JavaScript and TypeScript. You can import the client, and then use it in browser and/or server side JavaScript environments like node.js.

Direct Script Tag

Gadget generates JavaScript code directly for your web frontend for straightforward integrations into websites. This <script/> tag method is the simplest and easiest way to get started using Simple Blog Example's API.

html
<script src="https://simple-blog-example.gadget.app/api/client/web.min.js"></script>
<script>
window.api = new Gadget({ authenticationMode: { apiKey: "<your api key goes here>" } })
</script>

Once the above script tags have been included, you can access window.api (or just api for short) to start making API calls! The generated code is cached on Gadget's CDN and is safe to include directly on a high traffic web properties like a Shopify store or a Wordpress blog. We recommend this method of installation for websites when possible, as the client will update without you having to make changes to your code or manually sync with Gadget.

JavaScript
const results = await api.query(`
query GetSomeData {
# start writing GraphQL queries!
}
`);

Node Module

To work with the Gadget API server-side from node.js, or to include generated Gadget code in a larger product with a JavaScript bundler like Webpack or Parcel, you can install the npm module for Simple Blog Example. Installing the Simple Blog Example API client works just like any other NPM package Gadget, and works with any NPM-compatible package manager.

This installation method works by installing the package from Gadget's own npm registry, where we host, version, and update your API client.

To install the Simple Blog Example node module, register the Gadget NPM registry for the @gadget-client package scope:

npm config set @gadget-client:registry https://registry.gadget.dev/npm

Then, install the Simple Blog Example client package using a package manager:

npm install @gadget-client/simple-blog-example
yarn add @gadget-client/simple-blog-example

As you make changes to Simple Blog Example, Gadget will publish new versions of your API client to it's NPM registry. See Client Regeneration for more details on when updates are necessary.

You must register the Gadget npm registry in all environments where you want to use this package, which would include production systems, continuous integration environments, or build steps like Vercel or Netlify's static site builders. This can be done using the above npm config command, or by writing out an .npmrc file in those environments that points to Gadget for the @gadget-client scope.

After installation, the client module can be required and a client instance created:

JavaScript
1import { Client } from "@gadget-client/simple-blog-example";
2export const api = new Client({
3 authenticationMode: {
4 apiKey: "<your api key goes here>",
5 },
6});

The api object is now ready for use!

JavaScript
const results = api.query(`
query GetSomeData { # start writing GraphQL queries!
}
`);

Gadget also makes a tarball of the latest version of the API client available for download.

Tarball Endpoint
https://simple-blog-example.gadget.app/api/client/tarball

React

If you'd like to build a Gadget app with React, see the Using with React documentation.

Gadget recommends using the npm package installation for the generated API client when building with React. However, you must re-install the Gadget client if you make breaking changes to the application after installing the client. See Client Regeneration for more details.

Type Safety

The Gadget client package includes TypeScript types and full type safety for all the high level JavaScript functions like find and findMany. No special installation steps are necessary to take advantage of the type information that the Simple Blog Example has. For more information on using your client with TypeScript and the generated types, see Type Safety.

Client Regeneration

The API client source code changes as you make changes to your Gadget backend.

Because the web clients are generated on-demand, we recommend using these clients where possible. By sourcing it directly as a <script/> tag, your browser will fetch the most up to date code each time a page loads, and you'll always be working with a client guaranteed to work with the current API for the application. To preserve this fast refresh behavior, it is best to not cache the client with other infrastructure (like a proxy), or to save it to disk.

Updating the npm package

Gadget's npm registry will publish new versions of the npm package as changes are made and you must install the new version of the API client package locally to reflect these changes. If a new model is added or removed, it will be available in the generated clients right away, but you need to update your local dependency version to fetch these changes.

To upgrade using npm, run:

Update with npm
npm install @gadget-client/[email protected]

To upgrade using yarn, run:

Update with yarn
yarn add @gadget-client/[email protected]

Best Effort Functionality

If your client version is out of date, that version still makes a best effort to continue to work with new versions of the data model and GraphQL schema. If a breaking change is made however, the client may try to make requests which no longer work. This can always be remedied by updating any cached source code using npm or yarn, or by copying new files sourced from Gadget's web.js endpoint.

The following changes will break an out of date Simple Blog Example client and cause it to issue invalid requests:

  • changing a model's apiIdentifier
  • changing an action's apiIdentifier
  • removing an action
  • removing a model completely