Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target
Cargo.lock
**/*.rs.bk
.idea/
.vscode/
24 changes: 24 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "scalar-api"
version = "0.2.6"
edition = "2021"
rust-version = "1.75"
description = "API for managing Scalar platform resources."

[dependencies]
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2"
futures = "0.3"
bytes = "1"
url = "2"
tokio = { version = "1", features = ["time"] }

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[features]
default = ["rustls"]
rustls = ["reqwest/rustls-tls"]
native-tls = ["reqwest/native-tls"]
141 changes: 140 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,140 @@
# scalar-rust
# Scalar API

API for managing Scalar platform resources.

## TypeScript SDK

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

### Install

```bash
npm add @scalar/sdk
```

### Get a Scalar API key

Create an API key in your Scalar account:

- Dashboard: https://dashboard.scalar.com/account
- Store it in `.env`, for example:

```bash
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`.

```ts
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.

```ts
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

- [@scalar/sdk on npm](https://www.npmjs.com/package/@scalar/sdk)

## Installation

```toml
[dependencies]
scalar-api = "0.2.6"
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:

```rust,no_run
use scalar_api::Scalar;

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

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

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

```rust,ignore
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_api::Error`]. Match on the
result of `send().await` to distinguish API errors (with status and decoded
body) from transport and decoding failures:

```rust,ignore
use scalar_api::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`](./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](https://www.scalar.com/?utm_source=scalar-typescript-sdk-rust&utm_campaign=sdk)
Loading