Crates.io | surge-sdk |
lib.rs | surge-sdk |
version | 0.1.1-alpha.2 |
created_at | 2025-05-30 06:08:00.865823+00 |
updated_at | 2025-06-03 08:01:49.724282+00 |
description | Rust SDK for Surge.sh API - programmatically manage static site deployments, domains, SSL, and DNS |
homepage | |
repository | https://github.com/dantescur/surge-sdk |
max_upload_size | |
id | 1694734 |
size | 252,260 |
The surge-sdk is a Rust library for interacting with the Surge.sh API, enabling developers to programmatically manage static site deployments, domains, SSL certificates, DNS records, and more. Built with asynchronous Rust using reqwest and tokio, it provides a robust and type-safe interface for publishing projects, handling authentication, and retrieving analytics.
Add surge-sdk to your project by including it in your Cargo.toml:
[dependencies]
surge_sdk = "0.1.0"
Ensure you have Rust and Cargo installed. The library requires Rust 1.65 or later due to its use of modern async features.
Prerequisites
Basic Usage
use surge_sdk::{Config, SURGE_API, SurgeSdk, Auth, utils::generate_domain};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), surge_sdk::SurgeError> {
// Initialize the SDK
let config = Config::new(SURGE_API, "0.1.0")?;
let sdk = SurgeSdk::new(config)?;
let auth = Auth::Token("your-api-token".to_string());
// Publish a project
let project_path = Path::new("./my-project");
let domain = generate_domain(false);
let mut event_stream = sdk.publish(project_path, &domain, &auth, None, None).await?;
// Process events
while let Some(event) = event_stream.next().await {
match event {
Ok(event) => println!("Event: {}", event),
Err(e) => eprintln!("Error: {}", e),
}
}
Ok(())
}
This example configures the SDK, authenticates with a token, and publishes a project directory to a random generated domain, printing real-time events from the NDJSON stream.
use surge_sdk::{Config, SURGE_API, SurgeSdk, Auth, generate_domain};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), surge_sdk::SurgeError> {
let config = Config::new(SURGE_API, "0.1.0")?;
let sdk = SurgeSdk::new(config)?;
let auth = Auth::Token("your-api-token".to_string());
// Generate a random preview domain
let domain = generate_domain(true); // e.g., "happy-cat-1234.surge.sh"
let project_path = Path::new("./my-project");
// Publish WIP
let mut event_stream = sdk.publish_wip(project_path, &domain, &auth, None, None).await?;
while let Some(event) = event_stream.next().await {
println!("Event: {}", event?);
}
Ok(())
}
use surge_sdk::{Config, SURGE_API, SurgeSdk, Auth};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), surge_sdk::SurgeError> {
let config = Config::new(SURGE_API, "0.1.0")?;
let sdk = SurgeSdk::new(config)?;
let auth = Auth::Token("your-api-token".to_string());
let domain = "my-site.surge.sh";
let pem_path = Path::new("./certificate.pem");
sdk.ssl(domain, pem_path, &auth).await?;
println!("SSL certificate uploaded for {}", domain);
Ok(())
}
* Requires a Surge.sh Professional Account.**
The SurgeSdk provides a wide range of methods for interacting with the Surge.sh API. Key methods include:
Note: Features marked with
*
require a Surge.sh Professional Account.
Publishing:
Domain Management:
SSL and DNS:
Account and Analytics:
Miscellaneous:
See the API documentation https://docs.rs/surge-sdk for detailed method signatures and parameters.
The SDK provides a default API URL for convenience:
use surge_sdk::{Config, SURGE_API};
// Recommended: Use the built-in constant
let config = Config::new(SURGE_API, "0.1.0")?;
/*
* Note: The SURGE_API constant points to https://surge.surge.sh. Override it only
* if you need a custom endpoint (e.g., for testing).
*/
Usage:
use surge_sdk::{Config, SurgeSdk, SURGE_API};
let config = Config::new(SURGE_API, "0.1.0")?
.with_timeout(60) // Set timeout to 60 seconds
.with_insecure(true); // Allow invalid SSL certificates (for testing)
let sdk = SurgeSdk::new(config)?;
The SDK uses a unified SurgeError enum to handle errors, including HTTP, API, JSON, I/O, and event-related issues.
Example:
match sdk.publish(projectpath, domain, &auth, None, None).await {
Ok(stream) => { /\* process stream \_/ },
Err(surge_sdk::SurgeError::Api(api_err)) => eprintln!("API error: {:?}", api_err.errors),
Err(e) => eprintln!("Error: {}", e),
}
The SDK uses the log crate for detailed logging. Configure a logger (e.g., env_logger) to see debug output:
use env_logger;
env_logger::init(); // Enable logging with RUST_LOG=debug
Contributions are welcome! To contribute:
Please include tests and update documentation for new features.
Development Setup
git clone https://github.com/your-username/surge-sdk.git
cd surge-sdk
cargo build
cargo test
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or support, open an issue on GitHub or contact the maintainer at cesardaniel.9611@gmail.com. For bugs or feature requests, open an issue.