Skip to content
LogoLogo

Management API client

@picked/admin-api-client is a typed GraphQL client for the Management API. It gets the type of each result and its variables from the API schema.

import { createAdminApiClient, graphql } from '@picked/admin-api-client'
 
const MeQuery = graphql(`
  query Me {
    user {
      id
      email
    }
  }
`)
 
const client = createAdminApiClient({
  apiUrl: 'https://api.picked.app',
  apiKey: process.env.PICKED_API_KEY,
})
 
const data = await client.request(MeQuery, {})

Client options

createAdminApiClient(options) returns a client. The client sends each request to <apiUrl>/api/graphql.

OptionTypeDefaultDescription
apiUrlstringrequiredThe origin of the API, for example https://api.picked.app.
tokenstring | (() => string | Promise<string>)noneA bearer token. A function gives a new token for each request.
apiKeystringnoneAn API key. The client sends it in the X-API-Key header.
fetchtypeof fetchthe global fetchThe function that sends each request.

See Authentication for the tokens and keys that the API accepts.

Requests

client.request(document, variables, options?): Promise<Result>
ParameterTypeDescription
documentTadaDocumentNodeA document that graphql makes.
variablesobjectThe variables of the document. Use {} for a document without variables.
options.timeoutMsnumberThe time limit of the request in milliseconds. The default is 30000.
options.headersRecord<string, string>Headers for this request only. They replace the token and apiKey headers that have the same name.

The client does not check the shape of the response at runtime.

Errors

When the API refuses a request with a 429 status, the client waits about 1 second and sends the request again. It does this 3 times at most. options.timeoutMs includes these waits.

CaseError
The API returns GraphQL errorsAdminApiResponseError. Its codes holds the extensions.code of each error.
The API refuses the request with a 429 status 4 timesAdminApiRateLimitError
A network failure, a timeout, or an HTTP error without GraphQL errorsError

See Errors for each error code.

Workspace documents

The package also gives the documents that move a workspace from one project to a different project:

DocumentOperation
WorkspaceExportDocumentReads the full contents of a workspace.
ProjectLiveVersionDocumentReads the live version of a project.
WorkspaceImportDocumentMakes a workspace from those contents.
AssetUploadInitDocumentGives a URL that receives the file of an asset.
AssetCommitDocumentRecords an asset after its file is at that URL.
AssetDeleteDocumentDeletes an asset.
WorkspacePublishDocumentPublishes a workspace.

The picked workspace export and picked workspace import commands use these documents. See CLI.

App installation documents

The package also gives the documents that an app server sends as one installation. The installation client of @picked/app-kit sends them:

DocumentOperation
AppInstallationLabelsDocumentReads the labels of an installation.
LabelsSetDocumentMakes or replaces labels.
LabelsDeleteDocumentRemoves labels.
AppInstallationTokenCreateDocumentMakes an installation token. The request sends an app assertion in the picked-app-assertion header, through options.headers.