GetCurrentPriceByTokenPair
Retrieves the current price for a specified token pair.
Returns the mid-market price for the base token denominated in the quote token.
- Overview
- Protobuf
Method Options​
Authorisation specification of the GetCurrentPriceByTokenPair method.
| Type | METHOD_TYPE_READ |
|---|---|
| Access Level | METHOD_ACCESS_LEVEL_AUTHORISED |
| Roles |
|
Parameters​
Request and response parameter message overview:
Input: GetCurrentPriceByTokenPairRequest Message​
| Field | Type | Required | Description |
|---|---|---|---|
BaseToken |
| True | The base token for which a price is required. This is the token being priced (e.g., BTC in a BTC/USD pair). |
QuoteToken |
| True | The quote token in which the price should be denominated. This is the unit of measurement for the price (e.g., USD in a BTC/USD pair). |
Returns: Price Message​
syntax = "proto3";
package meshtrade.market_data.price.v1;
import "buf/validate/validate.proto";
import "meshtrade/market_data/price/v1/price.proto";
import "meshtrade/option/method_options/v1/method_options.proto";
import "meshtrade/type/v1/token.proto";
option go_package = "github.com/meshtrade/api/go/market_data/price/v1;price_v1";
option java_package = "co.meshtrade.api.market_data.price.v1";
/*
PriceService provides market data pricing information.
This service enables retrieval of current market prices for token pairs,
supporting trading decisions and portfolio valuation.
*/
service PriceService {
/*
Retrieves the current price for a specified token pair.
Returns the mid-market price for the base token denominated in the quote token.
*/
rpc GetCurrentPriceByTokenPair(GetCurrentPriceByTokenPairRequest) returns (meshtrade.market_data.price.v1.Price) {
option (meshtrade.option.method_options.v1.method_options) = {
type: METHOD_TYPE_READ
access_level: METHOD_ACCESS_LEVEL_AUTHORISED
roles: [
ROLE_MARKET_DATA_ADMIN,
ROLE_MARKET_DATA_VIEWER,
ROLE_MARKET_DATA_PRICE_ADMIN,
ROLE_MARKET_DATA_PRICE_VIEWER
]
};
}
}
/*
Request to retrieve the current price for a token pair.
*/
message GetCurrentPriceByTokenPairRequest {
/*
The base token for which a price is required.
This is the token being priced (e.g., BTC in a BTC/USD pair).
*/
meshtrade.type.v1.Token base_token = 1 [(buf.validate.field) = {required: true}];
/*
The quote token in which the price should be denominated.
This is the unit of measurement for the price (e.g., USD in a BTC/USD pair).
*/
meshtrade.type.v1.Token quote_token = 2 [(buf.validate.field) = {required: true}];
}
Code Examples​
Select supported SDK in the language of your choice for a full example of how to invoke the this method:
- Go
- Python
- Java
package main
import (
"context"
"log"
pricev1 "github.com/meshtrade/api/go/market_data/price/v1"
type_v1 "github.com/meshtrade/api/go/type/v1"
)
func main() {
ctx := context.Background()
// Default configuration is used and credentials come from MESH_API_CREDENTIALS
// environment variable or default discovery methods. Zero config required
// unless you want custom configuration.
service, err := pricev1.NewPriceService()
if err != nil {
log.Fatalf("Failed to create service: %v", err)
}
defer service.Close()
// Create request to get gold (XAU) price in ZAR
request := &pricev1.GetCurrentPriceByTokenPairRequest{
BaseToken: &type_v1.Token{
Code: "XAU", // Gold - the token to price
},
QuoteToken: &type_v1.Token{
Code: "ZAR", // South African Rand - the currency to price it in
},
}
// Call the GetCurrentPriceByTokenPair method
price, err := service.GetCurrentPriceByTokenPair(ctx, request)
if err != nil {
log.Fatalf("GetCurrentPriceByTokenPair failed: %v", err)
}
// Display the price information
log.Printf("Price retrieved successfully:")
log.Printf(" Base Token: %s", price.BaseToken.Code)
log.Printf(" Quote Token: %s", price.QuoteToken.Code)
log.Printf(" Mid Price: %s", price.MidPrice.Value)
log.Printf(" Timestamp: %s", price.Time.AsTime())
}
from meshtrade.market_data.price.v1 import (
GetCurrentPriceByTokenPairRequest,
PriceService,
)
from meshtrade.type.v1 import Token
def main():
# Default configuration is used and credentials come from MESH_API_CREDENTIALS
# environment variable or default discovery methods. Zero config required
# unless you want custom configuration.
service = PriceService()
with service:
# Create request to get gold (XAU) price in ZAR
request = GetCurrentPriceByTokenPairRequest(
base_token=Token(code="XAU"), # Gold - the token to price
quote_token=Token(code="ZAR"), # South African Rand - the currency to price it in
)
# Call the GetCurrentPriceByTokenPair method
price = service.get_current_price_by_token_pair(request)
# Display the price information
print("Price retrieved successfully:")
print(f" Base Token: {price.base_token.code}")
print(f" Quote Token: {price.quote_token.code}")
print(f" Mid Price: {price.mid_price.value}")
print(f" Timestamp: {price.time.ToDatetime()}")
if __name__ == "__main__":
main()
import co.meshtrade.api.market_data.price.v1.PriceService;
import co.meshtrade.api.market_data.price.v1.Service.GetCurrentPriceByTokenPairRequest;
import co.meshtrade.api.market_data.price.v1.Price.Price;
import co.meshtrade.api.type.v1.Token.Token;
import java.util.Optional;
public class GetCurrentPriceByTokenPairExample {
public static void main(String[] args) {
// Default configuration is used and credentials come from MESH_API_CREDENTIALS
// environment variable or default discovery methods. Zero config required
// unless you want custom configuration.
try (PriceService service = new PriceService()) {
// Create request to get gold (XAU) price in ZAR
GetCurrentPriceByTokenPairRequest request = GetCurrentPriceByTokenPairRequest.newBuilder()
.setBaseToken(Token.newBuilder()
.setCode("XAU") // Gold - the token to price
.build())
.setQuoteToken(Token.newBuilder()
.setCode("ZAR") // South African Rand - the currency to price it in
.build())
.build();
// Call the GetCurrentPriceByTokenPair method
Price price = service.getCurrentPriceByTokenPair(request, Optional.empty());
// Display the price information
System.out.println("Price retrieved successfully:");
System.out.println(" Base Token: " + price.getBaseToken().getCode());
System.out.println(" Quote Token: " + price.getQuoteToken().getCode());
System.out.println(" Mid Price: " + price.getMidPrice().getValue());
System.out.println(" Timestamp: " + price.getTime());
} catch (Exception e) {
System.err.println("GetCurrentPriceByTokenPair failed: " + e.getMessage());
e.printStackTrace();
}
}
}
Advanced Configuration​
For advanced client configuration options (custom endpoints, TLS settings, timeouts), see the SDK Configuration Guide.
Other Methods​
- Market Data Price v1 Method List - For Other methods