Code Examples
Working code examples in multiple programming languages to help you integrate quickly.
Authentication Scripts
The following scripts demonstrate the complete flow:
Note: Contact our team to obtain your
BASE_URL,UUID, andAPI_KEYcredentials before running these scripts.
- Password Login
- Certificate Request
- Certificate Login
- Authenticated API Call
Python
Requires requests and urllib3.
import requests
import json
from requests.adapters import HTTPAdapter
from urllib3.util.ssl_ import create_urllib3_context
# --- Configuration ---
BASE_URL = "https://47.236.197.241:6088/api/v1"
UUID = "your_uuid"
API_KEY = "your_api_key"
SNI_HOST = "loulilouwai.net"
# --- SNI Support Helper ---
class SNIAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = create_urllib3_context()
kwargs['ssl_context'] = context
kwargs['server_hostname'] = SNI_HOST
return super().init_poolmanager(*args, **kwargs)
# 1. Password Login
def password_login():
url = f"{BASE_URL}/system/login"
payload = {"name": UUID, "password": API_KEY}
response = requests.post(url, json=payload, verify=False) # Temp login
return response.json()['auth_token']
# 2. Request Certificate
def get_certificates(token):
url = f"{BASE_URL}/system/service/auth_cert"
headers = {"X-Molly-Wallet-Token": token}
payload = {"common_name": UUID, "alt_names": "namespace.srv"}
response = requests.post(url, json=payload, headers=headers, verify=False)
certs = response.json()
with open("ca.pem", "w") as f: f.write(certs['ca'])
with open("client.pem", "w") as f: f.write(certs['client_cert'])
with open("key.pem", "w") as f: f.write(certs['client_key'])
print("Certificates saved to disk.")
# 3. Certificate Login & Authenticated Call
def certificate_authenticated_call():
session = requests.Session()
session.mount('https://', SNIAdapter())
session.cert = ('client.pem', 'key.pem')
session.verify = 'ca.pem'
# Login with cert
login_url = f"{BASE_URL}/system/login"
login_payload = {"name": UUID, "password": ""}
login_resp = session.post(login_url, json=login_payload)
final_token = login_resp.json()['auth_token']
# Use final token for business API
headers = {"X-Molly-Wallet-Token": final_token}
biz_url = f"{BASE_URL}/your/business/endpoint"
response = session.get(biz_url, headers=headers)
return response.json()
if __name__ == "__main__":
tmp_token = password_login()
get_certificates(tmp_token)
result = certificate_authenticated_call()
print(result)Go
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
BaseURL = "https://47.236.197.241:6088/api/v1"
SNI = "loulilouwai.net"
)
func main() {
// 1. Password Login (Standard Client)
// ... (implementation omitted for brevity)
// 2. Load Certificates
cert, _ := tls.LoadX509KeyPair("client.pem", "key.pem")
caCert, _ := ioutil.ReadFile("ca.pem")
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// 3. Configure TLS with SNI
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
ServerName: SNI,
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
// 4. Certificate Login
loginBody, _ := json.Marshal(map[string]string{"name": "uuid", "password": ""})
resp, _ := client.Post(BaseURL+"/system/login", "application/json", bytes.NewBuffer(loginBody))
// ... extract final_token and call business APIs
}Node.js
const https = require('https');
const fs = require('fs');
const axios = require('axios');
const BASE_URL = 'https://47.236.197.241:6088/api/v1';
const SNI = 'loulilouwai.net';
const agent = new https.Agent({
cert: fs.readFileSync('client.pem'),
key: fs.readFileSync('key.pem'),
ca: fs.readFileSync('ca.pem'),
servername: SNI // This sets the SNI
});
async function makeAuthenticatedCall() {
// 1. Login with cert
const loginResp = await axios.post(`${BASE_URL}/system/login`,
{ name: 'uuid', password: '' },
{ httpsAgent: agent }
);
const finalToken = loginResp.data.auth_token;
// 2. Call business API
const bizResp = await axios.get(`${BASE_URL}/business/endpoint`, {
httpsAgent: agent,
headers: { 'X-Molly-Wallet-Token': finalToken }
});
console.log(bizResp.data);
}cURL (CLI)
Step 1: Password Login
curl -X POST https://47.236.197.241:6088/api/v1/system/login \
-H "Content-Type: application/json" \
-d '{"name": "<uuid>", "password": "<api_key>"}' \
--insecureStep 2: Request Certificate
curl -X POST https://47.236.197.241:6088/api/v1/system/service/auth_cert \
-H "X-Molly-Wallet-Token: <temp_token>" \
-H "Content-Type: application/json" \
-d '{"common_name": "<uuid>", "alt_names": "namespace.srv"}' \
--insecureStep 3: Certificate Login
# Save certs to client.crt, client.key, and ca.crt first
curl -X POST https://47.236.197.241:6088/api/v1/system/login \
--cert client.crt --key client.key --cacert ca.crt \
--resolve loulilouwai.net:6088:47.236.197.241 \
-H "Content-Type: application/json" \
-d '{"name": "<uuid>", "password": ""}'Next Steps
- Troubleshooting - Debugging connection and SSL issues
- Authentication Details - Deeper look at the mTLS flow
- API Reference - Explore all available endpoints
Updated 2 months ago
