GraphQL API
Overview
GraphQL is a query language for APIs that allows clients to request exactly the data they need. Unlike REST APIs, GraphQL provides a single endpoint where you can request multiple resources in a single query, reducing the number of network requests and giving you more control over the data you receive.
All data indexed by the Sapience API is available via the GraphQL endpoint. Explore what's available using the GraphQL sandbox in your web browser.
Endpoint
Sapience's GraphQL API is available at:
https://api.sapience.xyz/graphql
Querying the API
JavaScript/TypeScript
Using Apollo Client:
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://api.sapience.xyz/graphql',
cache: new InMemoryCache()
});
// Example query
const GET_DATA = gql`
query GetData {
// Your query here
}
`;
// Execute query
const { data } = await client.query({
query: GET_DATA
});
Python
Using gql
library:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
transport = RequestsHTTPTransport(
url='https://api.sapience.xyz/graphql'
)
client = Client(transport=transport, fetch_schema_from_transport=True)
// Example query
query = gql("""
query {
// Your query here
}
""")
result = client.execute(query)
cURL
curl -X POST \
-H "Content-Type: application/json" \
-d '{"query": "{ yourQuery }"}' \
https://api.sapience.xyz/graphql
Entities
Market Data
marketGroups
marketGroup
markets
marketCandles
positions
transactions
Auxiliary Data
categories
getMarketLeaderboard
totalVolumeByMarket
For Numeric Markets
indexPriceAtTime
indexCandles
resources
resource
resourceCandles
resourceTrailingAverageCandles
Example Queries
Get the questions and end times for the three Sapience markets that end next
query GetNextEndingMarkets {
markets(
first: 3,
orderBy: endTimestamp,
orderDirection: asc,
where: {
settled: false,
endTimestamp_gt: ${Math.floor(Date.now() / 1000)}
}
) {
marketId
question
endTimestamp
marketGroup {
question
address
chainId
}
}
}