Jump to content
Dan

Guide to using the Primedice API

Recommended Posts

Using the Primedice API
Graphql endpoint: https://api.primedice.com/graphql 

Authentication

You can authenticate with graphql via the http header. We utilise jwt token's for authentication which can be found via https://primedice.com/settings/tokens or for an unrestricted token (access to everything on your account) you can obtain it from your browser's local storage.

Authenticated requests require that you use this token in your header by adding it to the "x-access-token" property.

{
  "x-access-token": "token goes here"
}

 

Schema

Our schema is now locked down and private. To help support developers, I have created this topic to provide assistance to those who would like insights into certain queries or mutations. When someone requests details for a certain aspect of the schema, I will append this topic with the relevant details required to conduct such a query or mutation.

Terminology: With graphql, queries are the equivalent of a GET request, where you are asking for information to be returned, whereas a mutation is a POST request where you generally invoke an action.

 

API Client

When working with debugging queries using our API, theres a fair few API clients you can utilise to test that you are getting the responses you need. I currently use (and highly recommend) https://insomnia.rest/. It allows you to not only do standard REST requests with API's but also GraphQL requests. It's a great way to test that your queries are working before you throw them into your projects.

 

Examples

Send Message Mutation (sends a message to a chat channel)  [Authenticated]

mutation {
  sendMessage(chatId: "chat_id_goes_here", message: "message_goes_here") {
    id
    user {
      name
    }
  }
}

Public Chats Query (search for all public chat channels) [Unrestricted]

query {
  publicChats {
    id
    name
  }
}

Bet List Query (lookup a players bet history)  [Unrestricted]

{
  user(name: "Dan") {
    houseBetList(limit: 50, offset: 0) {
      id
      iid
      bet {
        ... on CasinoBet {
          game
          payout
          amountMultiplier
          payoutMultiplier
          amount
          currency
          createdAt
        }
      }
    }
  }
}

Bet Info Query (lookup a specific bet)  [Unrestricted]

{
  bet(iid: "house:32361565304") {
    id
    iid
    bet {
      ... on CasinoBet {
        game
        payout
        payoutMultiplier
        amount
        createdAt
        currency
        user {
          name
        }
        state {
          ... on CasinoGamePrimedice {
            result
            target
            condition
          }
        }
      }
    }
  }
}

Roll Dice (make a dice bet) [Authenticated]

mutation {
  primediceRoll(amount: 1, target: 50, condition: above, currency: doge) {
    id
    payout
    amountMultiplier
    payoutMultiplier
    createdAt
    nonce
  }
}

 

Please use this topic to submit a request on how to do a certain action with our API and I will ensure to get back to you with the necessary information.

Share this post


Link to post
Share on other sites
Posted (edited)

I don't really understand why does it have to be private, since pretty much all info available from sources, we just have to dig it out, but I respect your decision.
You should also remove the API button from the footer in that case.

Also I have to note your roll dice mutation returns your internal DB id and not the roll id. If someone wants to look up the roll id they have to use a separate query:

query Betiid($betId: String!) {
	bet(betId: $betId) {
		id
		iid
	}
}

I needed the Tip mutation, so here it is for those who might need it and don't want to waste time opening the sources:

mutation SendTipMutation(
	$userId: String!
	$amount: Float!
	$currency: CurrencyEnum!
	$isPublic: Boolean
	$chatId: String!
) {
	sendTip(
		userId: $userId
		amount: $amount
		currency: $currency
		isPublic: $isPublic
		chatId: $chatId
	) {
		id
		amount
		currency
		user {
			id
			name
		}
		sendBy {
			id
			name
			balances {
				available {
					amount
					currency
				}
			}
		}
	}
}

 

Edited by toohoola

Share this post


Link to post
Share on other sites

Hello,

I had used Primedice's API several years ago and want to start working with it again - but the switch to GraphQL has thrown me off.  This is the first time I've worked with GraphQL.  This topic seems to be the only documentation on the current API that I could find, and while I have figured out how to do simple things like retrieve a user's name and balances, I can't seem to figure out how to get the user's deposit address, or retrieve chat messages from a specific room...

 

I would love some guidance on this matter if possible.  Thank you!

Share this post


Link to post
Share on other sites
On 03/07/2019 at 13:43, toohoola said:

I don't really understand why does it have to be private, since pretty much all info available from sources, we just have to dig it out, but I respect your decision.
You should also remove the API button from the footer in that case.

Also I have to note your roll dice mutation returns your internal DB id and not the roll id. If someone wants to look up the roll id they have to use a separate query:


query Betiid($betId: String!) {
	bet(betId: $betId) {
		id
		iid
	}
}

I needed the Tip mutation, so here it is for those who might need it and don't want to waste time opening the sources:


mutation SendTipMutation(
	$userId: String!
	$amount: Float!
	$currency: CurrencyEnum!
	$isPublic: Boolean
	$chatId: String!
) {
	sendTip(
		userId: $userId
		amount: $amount
		currency: $currency
		isPublic: $isPublic
		chatId: $chatId
	) {
		id
		amount
		currency
		user {
			id
			name
		}
		sendBy {
			id
			name
			balances {
				available {
					amount
					currency
				}
			}
		}
	}
}

 

Thanks for adding the tip mutation that's really handy, and if the BetID issue could be resolved that'd be awesome @Dan or are you planning to leave it as is?

Share this post


Link to post
Share on other sites

@PrimediceSuite, the internet is full with GraphQL tutorials/guides/libraries, but if you don't want to search one, just fiddle with the tool that @Dan suggested in the first post. It can show you the raw HTTP data that has been sent and received. Based on that you should be able to use the API very similarly to the old API.

Share this post


Link to post
Share on other sites
Posted (edited)

Hey @toohoola,

I guess I phrased that weird.  I am getting familiar with GraphQL - I was more wondering what all the different queries and mutations are available to use, for example getting deposit addresses and chat messages.  I am having a hard time finding all the different queries I can use.

Edited by PrimediceSuite

Share this post


Link to post
Share on other sites

Hi @Dan,

2 moths ago (or so) this request was working:
 

{"operationName":null,"variables":{},"query":"mutation {\n  primediceRoll(amount: 0.001, target: 90.0, condition: above, currency: doge) {\n    ...BetFragment\n    ...PrimediceBetStateFragment\n    __typename\n  }\n}\n\nfragment BetFragment on Bet {\n  active\n  id\n  iid\n  payoutMultiplier\n  amountMultiplier\n  amount\n  payout\n  updatedAt\n  currency\n  game\n  nonce\n  serverSeed {\n    seedHash\n  }\n  clientSeed {\n    seed\n  }\n  user {\n    id\n    name\n    balances {\n      available {\n        currency\n        amount\n      }\n    }\n    __typename\n  }\n  __typename\n}\n\nfragment PrimediceBetStateFragment on Bet {\n  state {\n    ... on BetGamePrimedice {\n      result\n      target\n      condition\n      __typename\n    }\n    __typename\n  }\n  __typename\n}\n"}

Today it returns "HTTP Error 400: Bad Request"

This code is working ok:
 

{"operationName":"ActiveRaces","variables":{},"query":"query ActiveRaces {\n  activeRaces {\n    ...RaceFragment\n    __typename\n  }\n}\n\nfragment RaceFragment on Race {\n  id\n  name\n  currency\n  type\n  startTime\n  endTime\n  status\n  __typename\n}\n"}

so probably is problem only in query and the rest code is ok:

What was changed in API last weeks? How to fix roll dice request?
 

Share this post


Link to post
Share on other sites
On 05/08/2019 at 09:55, PrimediceSuite said:

Hello,

I had used Primedice's API several years ago and want to start working with it again - but the switch to GraphQL has thrown me off.  This is the first time I've worked with GraphQL.  This topic seems to be the only documentation on the current API that I could find, and while I have figured out how to do simple things like retrieve a user's name and balances, I can't seem to figure out how to get the user's deposit address, or retrieve chat messages from a specific room...

 

I would love some guidance on this matter if possible.  Thank you!

You can query the user object once you have authenticated (explained how to in original post.)

 

Example user query:

{
  user {
    id
    balances {
      available {
        currency
        amount
      }
      vault {
        currency
        amount
      }
    }
  }
}

 

On 05/08/2019 at 11:44, JstLikeMagyk said:

Thanks for adding the tip mutation that's really handy, and if the BetID issue could be resolved that'd be awesome @Dan or are you planning to leave it as is?

The betiid implementation was a decision made by the lead developers. It's certainly not ideal, but it's the only solution they could come too with the problems we were heading into.

10 hours ago, dicedice said:

Hi @Dan,

2 moths ago (or so) this request was working:
 


{"operationName":null,"variables":{},"query":"mutation {\n  primediceRoll(amount: 0.001, target: 90.0, condition: above, currency: doge) {\n    ...BetFragment\n    ...PrimediceBetStateFragment\n    __typename\n  }\n}\n\nfragment BetFragment on Bet {\n  active\n  id\n  iid\n  payoutMultiplier\n  amountMultiplier\n  amount\n  payout\n  updatedAt\n  currency\n  game\n  nonce\n  serverSeed {\n    seedHash\n  }\n  clientSeed {\n    seed\n  }\n  user {\n    id\n    name\n    balances {\n      available {\n        currency\n        amount\n      }\n    }\n    __typename\n  }\n  __typename\n}\n\nfragment PrimediceBetStateFragment on Bet {\n  state {\n    ... on BetGamePrimedice {\n      result\n      target\n      condition\n      __typename\n    }\n    __typename\n  }\n  __typename\n}\n"}

Today it returns "HTTP Error 400: Bad Request"

This code is working ok:
 


{"operationName":"ActiveRaces","variables":{},"query":"query ActiveRaces {\n  activeRaces {\n    ...RaceFragment\n    __typename\n  }\n}\n\nfragment RaceFragment on Race {\n  id\n  name\n  currency\n  type\n  startTime\n  endTime\n  status\n  __typename\n}\n"}

so probably is problem only in query and the rest code is ok:

What was changed in API last weeks? How to fix roll dice request?
 

iid is no longer available as a return field from a dice roll, if oyu remove it, then the mutaiton will work, as such:

mutation {
  primediceRoll(amount: 0.001, target: 90.0, condition: above, currency: doge) {
    id
    payout
    amountMultiplier
    payoutMultiplier
    createdAt
    nonce
  }
}

 

Share this post


Link to post
Share on other sites

Thanks @Dan,

I also changed Bet to CasinoBet and it works again.

Here is final code if somebody needs:

mutation {
  primediceRoll(amount: 0.001, target: 90.0, condition: above, currency: doge) {
    ...BetFragment
    ...PrimediceBetStateFragment
    __typename
  }
}

fragment BetFragment on CasinoBet {
  active
  id
  payoutMultiplier
  amountMultiplier
  amount
  payout
  updatedAt
  currency
  game
  nonce
  serverSeed {
    seedHash
  }
  clientSeed {
    seed
  }
  user {
    id
    name
    balances {
      available {
        currency
        amount
      }
    }
    __typename
  }
  __typename
}

  fragment PrimediceBetStateFragment on CasinoBet {
  state {
    ... on CasinoGamePrimedice {
      result
      target
      condition
      __typename
    }
    __typename
  }
  __typename
}


 

Share this post


Link to post
Share on other sites
On 08/08/2019 at 21:24, PrimediceSuite said:

What would the query look like to get a users deposit addresses?

What would the query look like to get chat messages from a certain room?

For deposit address:

query DepositAddress($currency: CurrencyEnum!) {
	info {
		currency(currency: $currency) {
			requiredConfirmations
		}
	}
	user {
		id
		depositAddress(currency: $currency) {
			id
			address
			currency
		}
	}
}

 

To get the chat messages you have 2 options, either get the history manually with this, using the limit and the offset parameters:

query PublicChats(
	$includeHistory: Boolean = false
	$limit: Int
	$offset: Int
) {
	publicChats {
		id
		name
		isPublic
		messageList(limit: $limit, offset:$offset) @include(if: $includeHistory) {
			...MessageFragment
		}
	}
}

or you can use subscription to get them as soon as possible:

subscription ChatSubscription($chatId: String!) {
	chatMessages(chatId: $chatId) {
		...MessageFragment
	}
}

The MessageFragment is the same in both cases, don't forget to attach it to your query. I left it out to save some space:

fragment MessageFragment on ChatMessage {
	id
	data {
		... on ChatMessageDataRace {
			race {
				name
				status
				startTime
				leaderboard(limit: 10) {
					...RacePositionFragment
				}
			}
		}
		... on ChatMessageDataTrivia {
			status
			question
			answer
			currency
			amount
			winner {
				id
				name
			}
		}
		... on ChatMessageDataText {
			message
		}
		... on ChatMessageDataBot {
			message
		}
		... on ChatMessageDataTip {
			tip {
				id
				amount
				currency
				sender: sendBy {
					id
					name
				}
				receiver: user {
					id
					name
				}
			}
		}
		... on ChatMessageDataRain {
			rain {
				amount
				currency
				rainUsers {
					user {
						id
						name
					}
				}
				giver: user {
					id
					name
				}
			}
		}
	}
	createdAt
	user {
		id
		name
	}
}

fragment RacePositionFragment on RacePosition {
	position
	user {
		id
		name
	}
	wageredAmount
	payoutAmount
	currency
}

 

Share this post


Link to post
Share on other sites

I am new to programing

Please can you tell 

How i ahutenticate with token in javascript 

And place bet in javascript 

In my browser counsel

I serched many sites on google but didn't help.

Share this post


Link to post
Share on other sites
On 03/07/2019 at 06:43, toohoola said:

I don't really understand why does it have to be private, since pretty much all info available from sources, we just have to dig it out, but I respect your decision.
You should also remove the API button from the footer in that case.

Also I have to note your roll dice mutation returns your internal DB id and not the roll id. If someone wants to look up the roll id they have to use a separate query:


query Betiid($betId: String!) {
	bet(betId: $betId) {
		id
		iid
	}
}

I needed the Tip mutation, so here it is for those who might need it and don't want to waste time opening the sources:


mutation SendTipMutation(
	$userId: String!
	$amount: Float!
	$currency: CurrencyEnum!
	$isPublic: Boolean
	$chatId: String!
) {
	sendTip(
		userId: $userId
		amount: $amount
		currency: $currency
		isPublic: $isPublic
		chatId: $chatId
	) {
		id
		amount
		currency
		user {
			id
			name
		}
		sendBy {
			id
			name
			balances {
				available {
					amount
					currency
				}
			}
		}
	}
}

 

 {
  "errors": [
    {
      "path": [
        "primediceRoll"
      ],
      "message": "You are not allowed to do that",
      "errorType": "permission",
      "data": {}
    }
  ],
  "data": null
}

I got this messege whenever i tyn the graphql code

Share this post


Link to post
Share on other sites
On 13/09/2019 at 01:13, Awais said:

I am new to programing

Please can you tell 

How i ahutenticate with token in javascript 

And place bet in javascript 

In my browser counsel

I serched many sites on google but didn't help.

You cant run graphql mutations and queries within your browser console, or at least its not advised.

How are you running the primedice roll mutation? You need to add the access token header to your code.

Share this post


Link to post
Share on other sites
On 19/09/2019 at 18:17, Awais said:

How the query  looks like to change the user's client or server seed

@Dan

Good question!

mutation {
  changeClientSeed(seed: "my1337newSeed") {
    id
    seed
    active
    createdAt
  }
}
mutation {
  rotateServerSeed {
    id
    seedHash
    active
    createdAt
  }
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×