leetcode_api_dart 0.0.1 copy "leetcode_api_dart: ^0.0.1" to clipboard
leetcode_api_dart: ^0.0.1 copied to clipboard

Leetcode API for Dart

leetcode_api_dart #

Inspired by https://github.com/dustyRAIN/leetcode-api-go

Get Started #

import 'package:leetcode_api_dart/leetcode_api_dart.dart';

final session = "your_leetcode_session_from_cookie";
final csrfToken = "your_leetcode_csrfToken_from_cookie";

void main() {
  final api = LeetcodeApiClient(
    session: session,
    csrfToken: csrfToken,
  );

  // make api calls
}
copied to clipboard

You can pass session and csrfToken as empty, however session info will provide additional data such as the user's solve count, submission status, premium data (if applicable to the user), etc.

  Future<Contest> getContestInfo({required String contestSlug});
  Future<ContestRanking> getContestRanking({required String contestSlug, required int page});
copied to clipboard
  Future<DiscussionList> getDiscussions({
    List<String> categories = const [],
    List<String> tags = const [],
    String? orderBy,
    String? searchQuery,
    required int offset,
  });

  Future<Discussion> getDiscussion({required int topicId});

  Future<List<Comment>> getDiscussionComments({
    required int topicId,
    String? orderBy,
    required int offset,
    required int pageSize,
  });

  Future<List<Comment>> getCommentReplies({required int commentId});
copied to clipboard
  Future<ProblemList> getAllProblems({required int offset, required int pageSize});
  Future<ProblemContent?> getProblemContentByTitleSlug({required String titleSlug});
  Future<ProbemsByTopics> getProblemsByTopic({required String topicTag});
  Future<ProblemList> getTopInterviewProblems({required int offset, required int pageSize});
copied to clipboard
  Future<UserPublicProfile> getUserPublicProfile({required String username});
  Future<TagProblemsCount> getUserSolveCountByProblemTag({required String username});
  Future<UserContestRankingDetails> getUserContestRankingHistory({required String username});
  Future<UserSolveCountByDifficultyDetails> getUserSolveCountByDifficulty({required String username});
  Future<UserCalendar> getUserProfileCalendar({required String username});
  Future<List<AcSubmission>> getUserRecentAcSubmissions({required String username, required int pageSize});
copied to clipboard

Make Your Own Call to Leetcode #

Indeed the available APIs may not be sufficient, hence a GraphQL request can be made directly. The following method will only allow us to make GET calls to prevent the misusage of the API. The method takes one parameter, which is GraphQL request payload as a string

import 'package:leetcode_api_dart/leetcode_api_dart.dart';

extension LeetcodeApiClientCustom on LeetcodeApiClient {
  Future<MyCustomModel> getCustomQuery({required String param}) async {
    final data = await makeGraphQLRequest(buildMyGraphQLQueryHere(param: param));
    return MyCustomModel.fromJson(data);
  }
}

// don't forget to add 'r' before the string, in order to pass the $ symbol without stripping it away in dart
String buildMyGraphQLQueryHere({required String param}) => r'''{
    "query": "\n query Something() {\n name \nis_blocked \n} \n",
    "variables": {}
  }''';
copied to clipboard

Tests #

In order to run tests, create a .env file in root directory

LEETCODE_SESSION_TOKEN=your_leetcode_session_from_cookie
LEETCODE_CSRF_TOKEN=your_leetcode_csrfToken_from_cookie
copied to clipboard
2
likes
100
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

2024.08.26 - 2025.07.21

Leetcode API for Dart

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

freezed_annotation, http, json_annotation

More

Packages that depend on leetcode_api_dart