Skip to content

feat: scalar-typescript-sdk-rust@0.2.2#3

Open
marclave wants to merge 1 commit into
mainfrom
scalar/scalar-typescript-sdk-rust/0.2.2
Open

feat: scalar-typescript-sdk-rust@0.2.2#3
marclave wants to merge 1 commit into
mainfrom
scalar/scalar-typescript-sdk-rust/0.2.2

Conversation

@marclave

Copy link
Copy Markdown
Member

Scalar API

API for managing Scalar platform resources.

TypeScript SDK

For TypeScript, we provide a SDK that makes using our API even easier.

Install

npm add @scalar/sdk

Get a Scalar API key

Create an API key in your Scalar account:

SCALAR_API_KEY=your_personal_token

Exchange your API key for an access token

The personal token is not an access token. Exchange it first with postv1AuthExchange.

If you use the personal token directly for authenticated API calls, the API returns 401 Invalid authentication token.

import { Scalar } from '@scalar/sdk'

const scalar = new Scalar()

const exchange = await scalar.auth.postv1AuthExchange({
  personalToken: process.env.SCALAR_API_KEY!,
})

const accessToken = exchange.accessToken

Use the access token

Construct a second client with bearer auth. Use this authenticated client for API calls.

import { Scalar } from '@scalar/sdk'

const scalar = new Scalar()

const exchange = await scalar.auth.postv1AuthExchange({
  personalToken: process.env.SCALAR_API_KEY!,
})

const authedScalar = new Scalar({
  bearerAuth: exchange.accessToken,
})

Notes

  • The exchange request itself can be made from a client constructed with no arguments (new Scalar()).
  • The exchanged access token is valid for 12 hours.
  • Timestamps are Unix seconds.

Read more

Installation

[dependencies]
scalar-scalar-typescript-sdk = "0.2.2"
tokio = { version = "1", features = ["full"] }

Usage

The client is asynchronous and built on tokio + reqwest. Construct it
with the builder, or read credentials from the environment:

use scalar_scalar_typescript_sdk::ScalarApiClient;

async fn run() -> Result<(), Box<dyn std::error::Error>> {
    let client = ScalarApiClient::builder()
        .bearer_token("…")
        .build()?;

    // Or, reading credentials from the environment:
    let client = ScalarApiClient::from_env()?;
    let _ = client;
    Ok(())
}

Every operation returns a request builder; set optional parameters fluently
and finish with .send().await:

let response = client.registry().list_all_api_documents().send().await?;

Authentication

Credentials can be set on the builder or read from the environment by
from_env:

  • bearer_token — environment variable SCALAR_BEARER_TOKEN

Error handling

Fallible operations return [scalar_scalar_typescript_sdk::Error]. Match on the
result of send().await to distinguish API errors (with status and decoded
body) from transport and decoding failures:

use scalar_scalar_typescript_sdk::Error;

match result {
    Ok(value) => { /* … */ }
    Err(Error::Api(api)) => eprintln!("status {}: {:?}", api.status, api.body),
    Err(other) => eprintln!("request failed: {other}"),
}

API reference

See api.md for the full list of resources and operations.

Contributions

This SDK is generated programmatically. Manual edits to generated files will be
overwritten on the next build.

SDK created by Scalar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants