Skip to main content

Using Our API in Your Project

If you have not used a GraphQL API before, we recommend reviewing at least the first few pages of the GraphQL tutorial on graphql.org to get familiar with the terminology and concepts.

It is strongly encouraged you use a GraphQL client-side library for your language. You can browse the available libraries on graphql.org's code page. Since GraphQL has special formatting and headers, you cannot send off a normal POST request easily like you would with a REST API. However, by using a library, you would be able to craft your query and then let the library handle all the GraphQL-specific parts.

We will be updating this page as we have more language-specific examples.

Python

For integrating from a project using Python, we recommend installing the python-graphql-client library. Then, you can pass any query or mutation you create (or copy from any of our guides) to the GraphqlClient.

For example, in order to use our API, you must first get an access token (see our getting started guide for details on that).

To do this in Python using python-graphql-client, you can copy our example for access tokens from that guide as the query like in this example:

from python_graphql_client import GraphqlClient

# Create your query or mutation.
query = """
mutation {
requestAccessToken(
input: {
grant_type: CLIENT_CREDENTIALS
client_id: "YOUR_ID"
client_secret: "YOUR_SECRET"
}
) {
access_token
}
}
"""

# Execute the GraphQL call using our API's endpoint and your query.
response = GraphqlClient(endpoint="https://api.podchaser.com/graphql").execute(query=query)

# Access the returned data.
access_token = response['data']['requestAccessToken']['access_token']

# Save the access token for re-use later.

Once you have your access token, you can use it when making requests to the API. For example, in this next code sample, we use the query from our getting started guide.

from python_graphql_client import GraphqlClient

# Retrieve your access token from whereever you saved it before.
acces_token = ''

# Prepare our query.
query = """
query {
podcasts {
data {
title,
description
}
}
}
"""

# Add our access token in the Authorization header.
headers = {
'Authorization': f'Bearer {access_token}'
}

# Execute the GraphQL call.
response = GraphqlClient(endpoint="https://api.podchaser.com/graphql", headers=headers).execute(query=query)

# Print out the first podcast's title.
print(response['data']['podcasts']['data'][0]['title'])

JavaScript

For integrating from a project using JavaScript, we recommend installing the graphql-request library. Then, you can pass any query or mutation you create (or copy from any of our guides) to the GraphQLClient.

For example, in order to use our API, you must first get an access token (see our getting started guide for details on that).

To do this in JavaScript using graphql-request, you can copy our example for access tokens from that guide as the query like in this example:

const { GraphQLClient, gql } = require('graphql-request');
const client = new GraphQLClient('https://api.podchaser.com/graphql');

const query = gql`
mutation getToken($client_id: String!, $client_secret: String!) {
requestAccessToken(
input: {
grant_type: CLIENT_CREDENTIALS
client_id: $client_id
client_secret: $client_secret
}
) {
access_token
token_type
}
}
`;
const variables = {
client_id: "", // Put your Client ID here or retrieve from a data store
client_secret: "" // Put your Client Secret here or retrieve from a data store
};

client.request(query, variables).then((response) => {
// Save the access token somewhere to be used in all future requests
console.log(response.requestAccessToken.access_token);
});

Once you have your access token, you can use it when making requests to the API. For example, in this next code sample, we use the query from our getting started guide.

const { GraphQLClient, gql } = require('graphql-request');

// Retrieve your access token from whereever you saved it before.
const acces_token = ''

const client = new GraphQLClient('https://api.podchaser.com/graphql', {headers: {
authorization: `Bearer ${access_token}`,
}});

const query = gql`
query {
podcasts {
data {
title,
description
}
}
}
`;

client.request(query).then((response) => {
// Print out the first podcast's title.
console.log(response.podcasts.data[0].title);
});