Skip to main content

Access to Graphql from code

GraphQL Endpoint

Finmars exposes GraphQL via a single endpoint.

:realm_code:/:space_code:/graphql/

This endpoint supports:

  • Queries
  • Mutations
  • Schema introspection

How to Access the Endpoint

Authentication

GraphQL uses the same authentication as the REST API.

Usually:


"DEFAULT_AUTHENTICATION_CLASSES": (   
   "poms.common.authentication.JWTAuthentication",    
   "poms.common.authentication.KeycloakAuthentication",
),


If you are not authenticated:

  • Requests will fail
  • Data will not be returned

From Code

Send a POST request to /graphql/.

import requests

url = "https://finmars.example.com/:realm_code:/:space_code:/graphql/"

headers = {
    "Authorization": "Bearer JWT_TOKEN",
    "Content-Type": "application/json"
}

query = """
query {
  account(pagination: {
    limit: 20
    offset: 0
  }) {
    id
    user_code
    name
    type {
      id
      name
      user_code
    }
  }
}
"""

payload = {
    "query": query
}

response = requests.post(url, json=payload, headers=headers)

if response.status_code == 200:
    print(response.json())
else:
    print(response.status_code, response.text)

And Response

{
  "data": {
    "account": [
      {
        "id": "11",
        "user_code": "129900RGLDPQ3DT2T5H1",
        "name": "Securities Vault Alpha",
        "type": {
          "id": "2",
          "name": "Default Account Type",
          "user_code": "com.finmars.standard-other:accounts.accounttype:default"
        }
      },
      {
        "id": "12",
        "user_code": "2398002D4FTG8TG3L9K7",
        "name": "Alpine Depository Services",
        "type": {
          "id": "2",
          "name": "Default Account Type",
          "user_code": "com.finmars.standard-other:accounts.accounttype:default"
        }
      }
    ]
  }
}


Well done!