# MarketsService

> Markets, market types and creating markets.

Source: https://docs.stxapp.io/sdks/go/reference/markets/

## MarketsService

```go
type MarketsService struct {
	// contains filtered or unexported fields
}
```

MarketsService lists markets and market types, and creates markets. Use it through [Client.Markets](/sdks/go/reference/client/#client).

### All

```go
func (s *MarketsService) All(ctx context.Context, q *MarketsQuery) iter.Seq2[Market, error]
```

All returns every market matching q, following the cursor. q.Cursor is ignored.

**Example**

List open markets with their best bid and offer.

```go
ctx := context.Background()
client, err := stx.New()
if err != nil {
	log.Fatal(err)
}
for market, err := range client.Markets.All(ctx, &stx.MarketsQuery{Status: []string{"open"}, Trading: stx.Bool(true)}) {
	if err != nil {
		log.Fatal(err)
	}
	var bid, offer stx.Decimal
	if len(market.Bids) > 0 {
		bid = market.Bids[0].Price
	}
	if len(market.Offers) > 0 {
		offer = market.Offers[len(market.Offers)-1].Price // sorted highest first
	}
	fmt.Printf("%s  bid %s  offer %s\n", market.Symbol, bid, offer)
}
```

### Create

```go
func (s *MarketsService) Create(ctx context.Context, eventID string, markets []NewMarket, opts *CreateMarketsOptions) ([]CreateMarketResult, error)
```

Create creates markets on a scheduled event (`POST /api/v1/markets`), 1 to [MaxMarketsPerRequest](/sdks/go/reference/types/#constants-and-variables) per call. It needs an app token with markets.write, which STX grants, sent from an address on the app's IP allow-list. See [https://docs.stxapp.io/apps/creating-markets/](https://docs.stxapp.io/apps/creating-markets/).

It returns one result per market, in order. A rejected market is a result, not an error, and resending is safe: a market already listed comes back MarketExists. A refused request is an error: a 403 without markets.write, [ErrNotFound](/sdks/go/reference/errors/#constants-and-variables) for an unknown event, [ErrRejected](/sdks/go/reference/errors/#constants-and-variables) for an event that is not open. Where the exchange cannot create markets the error matches [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables).

### Get

```go
func (s *MarketsService) Get(ctx context.Context, marketID string) (*Market, error)
```

Get returns one market by id. It returns an error matching [ErrNotFound](/sdks/go/reference/errors/#constants-and-variables) when there is none.

### List

```go
func (s *MarketsService) List(ctx context.Context, q *MarketsQuery) (*Page[Market], error)
```

List returns one page of markets (`GET /api/v1/markets`).

### Types

```go
func (s *MarketsService) Types(ctx context.Context) (MarketTypes, error)
```

Types returns the market types (`GET /api/v1/types`). It changes only when STX adds a rule or a stat, so read it once and keep it. It needs an API key or an OAuth app token with the events scope, and returns an error matching [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables) where the exchange does not have it.
