The RC API Client supports two authentication methods: Bearer token authentication and Basic authentication.
Authentication Enum
The Authentication enum defines the supported authentication methods:
#[derive( Debug , Clone )]
pub enum Authentication {
Bearer ( String ),
Basic { email : String , password : String },
}
impl Authentication {
fn to_header_value ( & self ) -> String {
match self {
Authentication :: Bearer ( token ) => format! ( "Bearer {}" , token ),
Authentication :: Basic { email , password } => {
let credentials = format! ( "{}:{}" , email , password );
let encoded = base64 :: engine :: general_purpose :: STANDARD . encode ( credentials );
format! ( "Basic {}" , encoded )
}
}
}
}
Variants
Authentication method for API requests. Bearer token authentication using a personal access token. Example: Authentication :: Bearer ( "your_token_here" . to_string ())
Basic
Basic { email: String, password: String }
Basic authentication using email and password. The credentials are base64-encoded automatically. Example: Authentication :: Basic {
email : "user@example.com" . to_string (),
password : "password" . to_string (),
}
Bearer Token Authentication
Bearer token authentication is the recommended method for most use cases. You can obtain a personal access token from your RC settings.
Creating a Client with Bearer Token
Using new()
Using with_auth()
From Environment Variable
use rc_api :: RcApiClient ;
let client = RcApiClient :: new ( "your_token_here" . to_string ()) ? ;
Real-World Example
From the RC VCF Generator:
let auth = rc_api :: Authentication :: Bearer (
std :: env :: var ( "RECURSE_PAT" ) . expect ( "RECURSE_PAT environment variable not set" ),
);
let client = rc_api :: RcApiClient :: with_auth ( auth ) . expect ( "Failed to create API client" );
Basic Authentication
Basic authentication uses your RC email and password. This method is required for certain operations like creating tokens.
Creating a Client with Basic Auth
Using with_basic_auth()
Using with_auth()
use rc_api :: RcApiClient ;
let client = RcApiClient :: with_basic_auth (
"user@example.com" . to_string (),
"password" . to_string (),
) ? ;
Base64 Encoding
The client automatically encodes your credentials to base64 format:
// Input: email="user@example.com", password="password"
// Output: "Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZA=="
Basic authentication credentials are base64-encoded automatically. You don’t need to encode them yourself.
When to Use Each Method
Use Bearer token authentication when:
You have a personal access token
You’re building a production application
You want to avoid hardcoding passwords
You’re accessing read-only data (profiles, batches, etc.)
Advantages:
More secure (can be revoked without changing password)
Can be scoped to specific permissions
Recommended for most use cases
Use Basic authentication when:
You need to create new tokens
You’re performing administrative operations
You don’t have a token yet
Required for:
Creating personal access tokens via create_token()
Considerations:
Less secure than tokens
Should be used with HTTPS only
Consider using environment variables for credentials
The client automatically adds the Authorization header to all requests:
fn auth_headers ( & self ) -> HeaderMap {
let mut headers = HeaderMap :: new ();
headers . insert (
AUTHORIZATION ,
HeaderValue :: from_str ( & self . auth . to_header_value ()) . unwrap (),
);
headers
}
Authorization : Bearer your_token_here
Security Best Practices
Never commit tokens or passwords to version control. Always use environment variables or secure secret management.
Using Environment Variables
Create a .env file:
RECURSE_PAT = your_token_here
Load it in your application:
use dotenv :: dotenv;
fn main () {
dotenv () . ok ();
let token = std :: env :: var ( "RECURSE_PAT" )
. expect ( "RECURSE_PAT not set" );
let client = RcApiClient :: new ( token ) ? ;
}
.gitignore
Make sure to ignore your .env file:
Testing Authentication
You can test your authentication by calling get_my_profile():
use rc_api :: RcApiClient ;
let client = RcApiClient :: new ( "your_token" . to_string ()) ? ;
match client . get_my_profile () {
Ok ( profile ) => println! ( "Authenticated as: {}" , profile . name),
Err ( e ) => eprintln! ( "Authentication failed: {}" , e ),
}
Next Steps
Create Tokens Learn how to create personal access tokens
Error Handling Handle authentication errors
Profiles API Start using the authenticated client