GraphQL
Get started with the Lens GraphQL API.
The Lens API utilizes GraphQL, a query language for APIs that allows clients to precisely request the data they need, leading to more efficient data retrieval. GraphQL queries are executed using a type system defined for your data. It is written in rust so it is blazing fast.
Environments
The API links below act as API endpoints as well as playgrounds for testing queries.
Network | URL | |
---|---|---|
Lens API Mainnet | https://api.lens.xyz/graphql | |
Lens API Testnet | https://api.testnet.lens.xyz/graphql |
Rate Limits
If you require higher rate limits when using the Lens API in a server-to-server context, you can generate a Server API Key for your application in the Lens Developer Dashboard. Include this key in your requests using the x-lens-app header.
DO NOT use the Server API Key in a client-side context. It is meant for server-to-server communication only.
HTTP
POST /graphqlnew HTTP/1.1Host: api.lens.xyzx-lens-app: <YOUR_API_KEY>
...
GraphQL Clients
Below are examples of how to interact with the Lens API using different GraphQL clients.
If you are connecting to the Lens API from a JavaScript or TypeScript environment, we recommend using the Lens TypeScript SDK. Use one of the examples below if you need more control over your GraphQL queries.
- URQL
- Apollo Client
- Fetch API
The URQL client is a lightweight and flexible GraphQL client that can be used in both web and mobile applications.
urql.ts
import { gql, Client, cacheExchange, fetchExchange } from "urql";
const ENDPOINT = "https://api.lens.xyz/graphql";
const client = new Client({ url: ENDPOINT, exchanges: [cacheExchange, fetchExchange],});
const result = await client.query(gql` query { posts(request: { pageSize: TEN }) { items { id author { username { value } } metadata { ... on TextOnlyMetadata { content } } } pageInfo { prev next } } }`);
console.log(result);