https://github.com/ksysoev/deriv-api
Golang Deriv API client
https://github.com/ksysoev/deriv-api
api deriv trading
Last synced: 10 months ago
JSON representation
Golang Deriv API client
- Host: GitHub
- URL: https://github.com/ksysoev/deriv-api
- Owner: ksysoev
- License: mit
- Created: 2023-03-15T12:14:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-10T08:04:44.000Z (10 months ago)
- Last Synced: 2025-09-10T11:50:23.526Z (10 months ago)
- Topics: api, deriv, trading
- Language: Go
- Homepage:
- Size: 1.76 MB
- Stars: 17
- Watchers: 3
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deriv Api Client
[](https://github.com/ksysoev/deriv-api/actions/workflows/main.yml)
[](https://codecov.io/gh/ksysoev/deriv-api)
[](https://github.com/ksysoev/deriv-api/actions/workflows/schema.yml)
[](https://goreportcard.com/report/github.com/ksysoev/deriv-api)
[](https://pkg.go.dev/github.com/ksysoev/deriv-api)
[](https://opensource.org/licenses/MIT)
**This is unofficial library for working with [Deriv API](https://api.deriv.com)**
DerivAPI is a Go package that provides a client for the Deriv API. It allows you to connect to the Deriv API and make requests to retrieve data and perform actions on your or your client's Deriv account.
## Installation
To use DerivAPI in your Go project, you can install it using go get:
```
go get github.com/ksysoev/deriv-api
```
## Usage
### Tick stream
Here's an example of how to use DerivAPI to connect to the Deriv API and subscribe on market data updates:
```golang
import (
"context"
"fmt"
"log"
"time"
"github.com/ksysoev/deriv-api"
"github.com/ksysoev/deriv-api/schema"
)
api, err := deriv.NewDerivAPI("wss://ws.binaryws.com/websockets/v3", 1, "en", "http://example.com/")
if err != nil {
log.Fatal(err)
}
defer api.Disconnect()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, sub, err := api.SubscribeTicks(ctx, schema.Ticks{Ticks: "R_50"})
if err != nil {
log.Fatal(err)
return
}
fmt.Println("Symbol: ", *resp.Tick.Symbol, "Quote: ", *resp.Tick.Quote)
for tick := range sub.Stream {
fmt.Println("Symbol: ", *tick.Tick.Symbol, "Quote: ", *tick.Tick.Quote)
}
```
### Buying a contract
Here's another example of how to use DerivAPI to buy contract and listen for updates for this contract.
```golang
import (
"context"
"log"
"time"
"github.com/ksysoev/deriv-api"
"github.com/ksysoev/deriv-api/schema"
)
apiToken := "YOU_API_TOKEN_HERE"
api, err := deriv.NewDerivAPI("wss://ws.binaryws.com/websockets/v3", 1, "en", "https://www.binary.com")
if err != nil {
log.Fatal(err)
}
defer api.Disconnect()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// First, we need to authorize the connection
reqAuth := schema.Authorize{Authorize: apiToken}
_, err = api.Authorize(ctx, reqAuth)
if err != nil {
log.Fatal(err)
}
amount := 100.0
barrier := "+0.001"
duration := 5
basis := schema.ProposalBasisPayout
reqProp := schema.Proposal{
Proposal: 1,
Amount: &amount,
Barrier: &barrier,
Basis: &basis,
ContractType: deriv.ProposalContractTypeCALL,
Currency: "USD",
Duration: &duration,
DurationUnit: deriv.ProposalDurationUnitT,
Symbol: "R_50",
}
// Send a proposal request
proposal, err := api.Proposal(ctx, reqProp)
if err != nil {
log.Fatal(err)
}
buyReq := schema.Buy{
Buy: proposal.Proposal.Id,
Price: 100.0,
}
_, buySub, err := api.SubscribeBuy(context.Background(), buyReq)
if err != nil {
log.Fatal(err)
}
for proposalOpenContract := range buySub.Stream {
log.Println("Current Spot: ", *proposalOpenContract.ProposalOpenContract.CurrentSpot)
if *proposalOpenContract.ProposalOpenContract.IsSold == 1 {
log.Println("Contract Result: ", proposalOpenContract.ProposalOpenContract.Status.Value)
buySub.Forget()
break
}
}
```