GraphQL API
Most of the Liveforce API is built using GraphQL, a powerful query language for APIs that allows you to request only the data you need. This section provides an overview of all available GraphQL queries and mutations, along with explanations and examples.
Endpoint
All GraphQL requests should be made to the following endpoint:
https://api.liveforce.co/gql
Schema
The entire schema can be downloaded here. You can also use the following link to import the schema into your GraphQL client:
https://docs.liveforce.co/generated/liveforce-api-schema.graphql
Usage
- Node.js (Apollo)
- Go (genqlient)
- cURL
- Other
Here's an example of a request to GraphQL API using Apollo Client, a popular GraphQL client for JavaScript and TypeScript.
import { ApolloClient, InMemoryCache, gql, HttpLink } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
const accessToken = "your_access_token_here";
const httpLink = new HttpLink({
uri: "https://api.liveforce.co/0.2/gql",
});
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
Authorization: `Bearer ${accessToken}`,
},
};
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
async function main() {
const { data } = await client.query({
query: gql`
query GetUpdatedCrew($time: Time!) {
companyCrewAll(input: { filter: { updatedAfter: $time } }) {
paginatedList {
crewId
firstName
lastName
email
}
}
}
`,
variables: { time: "2020-11-18T12:21:14Z" },
});
console.log("Retrieved", data.companyCrewAll.paginatedList.length, "crew members");
}
main();
genqlient is a type-safe GraphQL client for Go. It generates Go types and helper functions based on your GraphQL queries and schema, allowing you to interact with a GraphQL API in a type-safe manner.
Here's a .graphql file containing query definition:
query GetUpdatedCrew($time: Time!) {
companyCrewAll(input: {filter: {updatedAfter: $time}}) {
paginatedList {
crewId
firstName
lastName
email
}
}
}
genqlient configuration file:
schema: "liveforce-api-schema.graphql"
operations:
- "get-updated-crew.graphql"
generated: "generated.go"
bindings:
Time:
type: "string"
Go code, performing the query using generated by genqlient types:
package main
import (
"context"
"github.com/Khan/genqlient/graphql"
"log"
"net/http"
)
func main() {
accessToken := "your_access_token_here"
client := &http.Client{
Transport: &AuthorizedTransport{
RoundTripper: http.DefaultTransport,
Token: accessToken,
},
}
graphqlClient := graphql.NewClient("https://api.liveforce.co/gql", client)
response, err := GetUpdatedCrew(context.TODO(), graphqlClient, "2020-11-18T12:21:14Z")
if err != nil {
log.Fatalf("error getting updated crew: %v", err)
}
log.Printf("Retrieved %d crew members", len(response.CompanyCrewAll.PaginatedList))
}
type AuthorizedTransport struct {
http.RoundTripper
Token string
}
func (t *AuthorizedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+t.Token)
return t.RoundTripper.RoundTrip(req)
}
Minimal example of a GraphQL API request using curl:
#!/bin/bash
ACCESS_TOKEN="your_access_token_here"
curl -X POST "https://api.liveforce.co/gql" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
--data '{
"query": "query GetUpdatedCrew($time: Time!) { companyCrewAll(input: { filter: { updatedAfter: $time } }) { paginatedList { crewId firstName lastName email } } }",
"operationName": "GetUpdatedCrew",
"variables": { "time": "2020-11-18T12:21:14Z" }
}'
You can use any GraphQL client or HTTP library to interact with the GraphQL API. There is a catalogue of available GraphQL tools and libraries to help you choose the right one for your needs.
When making requests, remember to include the Authorization header with a valid access token.