Creating/Deleting Blockchains
You can manage your blockchains through the UI at mirror.ad or via API
Creating a Mirror instance
Use this endpoint to create a new Mirror instance. You can use the mirror url in any place you would use an RPC URL. On the free beta, you are allowed up to 10 Mirror instances. Don't fret though because you can delete unused instances at will!
async function getRpcUrl(): Promise<string> {
const mirrorApiKey = process.env.MIRROR_API_KEY;
try {
const response = await fetch("https://rpc.mirror.ad/blockchains", {
method: "POST",
headers: {
api_key: mirrorApiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
if (response.status !== 200) {
throw new Error(`Error getting RPC URL: ${response.status}`);
}
const data = (await response.json()) as { url: string };
return data.url;
} catch (error) {
console.error("Error:", error);
throw error;
}
}
use reqwest::Client;
use serde::Deserialize;
use std::env;
use tokio;
#[derive(Deserialize)]
struct RpcResponse {
url: String,
}
async fn get_rpc_url() -> Result<String, Box<dyn std::error::Error>> {
let mirror_api_key = env::var("MIRROR_API_KEY")?;
let client = Client::new();
let response = client
.post("https://rpc.mirror.ad/blockchains")
.header("api_key", mirror_api_key)
.header("Content-Type", "application/json")
.send()
.await?;
if response.status() != 200 {
return Err(format!("Error getting RPC URL: {}", response.status()).into());
}
let data: RpcResponse = response.json().await?;
Ok(data.url)
}
func getRpcUrl() (string, error) {
godotenv.Load()
mirrorApiKey := os.Getenv("MIRROR_API_KEY")
req, err := http.NewRequest("POST", "https://rpc.mirror.ad/blockchains", bytes.NewBuffer([]byte{}))
if err != nil {
return "", err
}
req.Header.Set("api_key", mirrorApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("Error getting RPC URL: %s", resp.Status)
}
type res struct {
Url string `json:"url"`
}
var r res
err = json.Unmarshal(body, &r)
if err != nil {
return "", err
}
return r.Url, nil
}
NOTE: You don't need an api-key to hit the rpc endpoint, just use it as any RPC URL
Deleting a Mirror instance
Deleting a mirror-chain instance is incredibly easy!
async function clearMirrorchain(rpcUrl: string): Promise<void> {
const mockchainApiKey = process.env.MIRROR_API_KEY;
try {
const response = await fetch(rpcUrl, {
method: "DELETE",
headers: {
api_key: mockchainApiKey,
"Content-Type": "application/json",
},
});
const body = await response.text();
if (response.status !== 200) {
throw new Error(`Error clearing mockchain: ${body}`);
}
} catch (error) {
console.error("Error:", error);
throw error;
}
}
use reqwest::Client;
use std::env;
async fn clear_mirrorchain(rpc_url: &str) -> Result<(), Box<dyn std::error::Error>> {
let mockchain_api_key = env::var("MIRROR_API_KEY")?;
let client = Client::new();
let response = client
.delete(rpc_url)
.header("api_key", mockchain_api_key)
.header("Content-Type", "application/json")
.send()
.await?;
if response.status() != 200 {
let body = response.text().await?;
return Err(format!("Error clearing mockchain: {}", body).into());
}
Ok(())
}
func clearMirrorchain(rpcUrl string) error {
godotenv.Load()
mockchainApiKey := os.Getenv("MIRROR_API_KEY")
req, err := http.NewRequest("DELETE", rpcUrl, nil)
if err != nil {
return err
}
req.Header.Set("api_key", mockchainApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return fmt.Errorf("Error clearing mockchain: %s", string(body))
}
return nil
}
Last updated