Skip to main content

Crate fopost

Crate fopost 

Source
Expand description

Official Rust SDK for the FoPost API — schedule and publish to +30 social platforms from your code.

use fopost::{Client, models::CreatePost};

let client = Client::from_env()?;

let workspaces = client.workspaces().list().await?;
let workspace = &workspaces[0];

let accounts = client.accounts().list(Some(&workspace.id)).await?;
let ids: Vec<_> = accounts.iter().map(|a| a.id.clone()).collect();

// Create a draft, then send it out.
let post = client
    .posts()
    .create(&CreatePost::text(&workspace.id, "Hello from Rust").accounts(ids))
    .await?;
client.posts().publish(&post.id, &Default::default()).await?;

§Authentication

Every request sends an API key in X-API-Key. Keys are created in the dashboard under Settings → API Keys and carry only the scopes granted at creation: posts (which covers publishing, deliveries, and media), workspaces, accounts, labels, webhooks, analytics, automations, inbox, and ads (whose four spending calls also need publish). A key may also be bound to one workspace, in which case naming any other workspace answers 403.

§Errors

Everything returns Result<T>. A non-2xx response becomes Error::Api, carrying the status, the API’s machine-readable code, and the whole body:

match client.posts().publish("9b2f6c1e-…", &Default::default()).await {
    Ok(result) => println!("{} deliveries queued", result.deliveries().len()),
    Err(fopost::Error::Api(err)) if err.is_payment_required() => {
        println!("upgrade at {:?}", err.upgrade_url());
    }
    Err(err) => eprintln!("{err}"),
}

§Rate limiting

Requests are limited per key, per minute. A 429 is retried automatically, waiting for the interval the API asks for in Retry-After, up to ClientBuilder::max_retries attempts. Set it to 1 to handle backoff yourself.

§Cargo features

FeatureDefaultWhat it does
rustls-tlsyesTLS through rustls, needing no system OpenSSL
native-tlsnoTLS through the platform’s own stack
multipartyesMedia upload and bulk CSV import

Re-exports§

pub use error::ApiError;
pub use error::Error;
pub use error::Result;
pub use http::DEFAULT_BASE_URL;

Modules§

error
Errors returned by the SDK.
http
Internal transport: auth headers, JSON coding, retries, envelope unwrap.
models
Types for every request body and response the SDK models.
resources
One module per API group. Reach them through the crate::Client.

Structs§

Client
A client for the FoPost API.
ClientBuilder
Builds a Client.

Constants§

API_KEY_ENV
The environment variable the key is read from by Client::from_env.

Type Aliases§

FoPost
Alias for people arriving from the TypeScript SDK, where the class is FoPost.