Authentication
How to authenticate your API requests to Droplit.
API Authentication
All API requests to your Droplit instance must be authenticated using an X-Auth-Token header.
This token is generated by signing a standardized message using your Bitcoin private key (WIF). The message typically consists of the request path and, if applicable, a hash of the request body. The signature scheme used is Bitcoin Signed Message (BSM), which is the default for this application, though the underlying libraries support other schemes.
Recommended Libraries
We strongly recommend using the open-source bitcoin-auth libraries to handle token generation. They simplify the process of creating the correct signature and formatting the token.
Example: Generating a token with bitcoin-auth (TypeScript/JavaScript)
import { AuthConfig, getAuthToken } from 'bitcoin-auth'; // Corrected to getAuthToken
async function generateDroplitApiToken() {
const privateKeyWif = "your-private-key-wif"; // Replace with your WIF
const requestPath = "/faucets/your-faucet-name/push-data"; // Example path
const body = JSON.stringify({ message: "Hello Droplit!" }); // Example body
const authConfig: AuthConfig = {
privateKeyWif: privateKeyWif,
requestPath: requestPath,
body: body, // For GET requests or requests with no body, omit this or pass an empty string
// scheme defaults to 'bsm' in this version of bitcoin-auth, which matches Droplit's expectation
// If you needed to be explicit or use a different scheme supported by the library:
// scheme: 'bsm'
};
try {
const token = await getAuthToken(authConfig); // Corrected to getAuthToken
console.log("Generated X-Auth-Token:", token);
// Use this token in the X-Auth-Token header of your API request
} catch (error) {
console.error("Error generating token:", error);
}
}
generateDroplitApiToken();Example: Generating a token with go-bitcoin-auth (Go)
package main
import (
"fmt"
auth "github.com/b-open-io/go-bitcoin-auth"
)
func main() {
privateKeyWif := "your-private-key-wif" // Replace with your WIF
requestPath := "/faucets/your-faucet-name/push-data" // Example path
bodyString := `{"message":"Hello Droplit!"}` // Example body (omit for GET)
config := auth.AuthConfig{
PrivateKeyWIF: privateKeyWif,
RequestPath: requestPath,
Body: bodyString,
Scheme: auth.SchemeBSM, // Specify BSM scheme explicitly if preferred, though Droplit uses this by default
}
token, err := auth.GetAuthToken(config)
if err != nil {
fmt.Println("Error generating token:", err)
return
}
fmt.Println("Generated X-Auth-Token:", token)
// Use this token in the X-Auth-Token header of your API request
}Remember to replace placeholder values like your-private-key-wif and /faucets/your-faucet-name/push-data with your actual WIF and the correct API endpoint path for your Droplit instance.